How To Create And Remove Directories in Linux With RMDIR and MKDIR (2024)

  • Christian Vasquez
  • No Comments

Table of Contents

Directory management on Linux is an easy task with the use of mkdir, which creates directories and rmdir which removes them. mkdir & rmdir are essential commands to know, especially if you want to get comfortable with the Linux CLI. You don’t want to be GUI jockey, now…

Create a directory in Linux

Creating a directory is a simple affair, simply call mkdir followed by the name you wish to give it. This will create a directory in the present working directory.

mkdir mydir

You can enter any number of directories alongside each other, they just need to be separated by a space and they will be created together.

mkdir mydir1 mydir2 mydir3

mkdir options

mkdir can also create nested directories, it just requires the path and passing the -p option. -p tells mkdir to create the parent directory if it does not exist. For example:

mkdir my/nested/dir -p

If you attempt the above without -p, you’ll get an error stating that mkdir: cannot create directory ‘my/nested/dir’: No such file or directory.

Another option of note is -v, which makes mkdir output verbose information about the current operation, useful for troubleshooting if your output has errors for any reason.

Remove a directory in Linux

To get rid of unwanted directories we can use rmdir.

rmdir mydir

Just like with mkdir we can enter many names to be removed, just separate them by a space.

rmdir mydir1 mydir2 mydir3

Note: rmdir will only work on empty directories. For non-empty directories, you will need to use rm -rf to remove the directory its contents. For more info on rm check out our post, linked here.

rmdir options

rmdir also uses -p and -v options and they work identically, only this time -p will remove the parent directory (but only if you provide the file path).

rmdir another/nested/dir -pv

Using wildcards with rmdir

rmdir can make use of the wildcard *, which allows for batch removal operations. For removing a nested series of directories with a depth of 3 we could do this;

rmdir */*/* -pv

As noted above if the directory has anything other than nested directories, then rmdir will fail. Below you can see an example file structure, and the resulting failure.

tree.└── another └── nested └── dir └── somefile.txtrmdir */*/* -pv rmdir: removing directory, 'another/nested/dir'rmdir: failed to remove 'another/nested/dir': Directory not empty

This version of the command really only works if you have an idea of how the directories are nested and they have to be empty(in order to type out the right number of wildcards), so this command is very situational.

Conclusion

mkdir and rmdir are absolute necessities when it comes to basic file operations on a Linux server/system. mkdir more so than rmdir, as rmdir is too situational, and rm does its job far better. Either way directory management via the CLI is not so tough once you got these commands under your belt. Thanks for reading and checkout our other posts for more command line literacy.

How To Install and Use UFW Firewall In Linux

Boyd GordonJanuary 12, 2022

UFW, the Uncomplicated Firewall, is as true as a command line utility can be. Posing

View Post

Top 5 Best Bootable USB Creators For Linux

Boyd GordonApril 21, 2023

So you have a flourishing need to get wrist deep into, “what is the Linux

View Post

Quickly Learn To Use The CAT Command In Linux

Omar MartinezApril 11, 2023

The “cat” command in Linux is a powerful tool that allows users to create, view,

View Post

Important basic Linux commands for absolute beginners – (Part 1)

Omar MartinezAugust 18, 2021

So you want to learn Linux? We can help you get there by getting you

View Post

How To Add User To Sudoers File In Linux

Boyd GordonMay 12, 2023

The interesting thing about your sudoers file is that you can edit it in a

View Post

Fixing Installation Mistakes in Arch Linux with Arch-Chroot

Boyd GordonJune 30, 2022

If you have ever installed Arch before, (especially if you are new) then you know

View Post

Using An Alias On Linux With 5 Easy Examples!

Christian VasquezFebruary 17, 2023

Tired of running the same command many times? Wield the power of an alias on

View Post

The Pacman Cheatsheet That Will Make Your Life Better

Christian VasquezMay 30, 2022

Here’s one for the ArchArch-based gang! Today I’ll be covering basic pacman commands that you

View Post

Best Linux Image Editors to Unleash your Creativity With

John BorrelliFebruary 4, 2022

The term Photoshop has now become a type of genericization (a brand name that is

View Post

Top 10 Google Drive Alternatives That Care About Privacy

John BorrelliJanuary 27, 2022

We are looking at free Google Drive Alternatives that provide desktop synchronization with your Linux

View Post

Meet the Author

How To Create And Remove Directories in Linux With RMDIR and MKDIR (11)

Christian Vasquez

Homelabber and Linux fan since 2017.

Posts by Christian Vasquez

How To Create And Remove Directories in Linux With RMDIR and MKDIR (13)

Using Azuracast To Host An Internet Radio Station

Self hosting AzuraCast gives you full control over your online radio station, allowing you to customize the experience for your listeners and save money on

View Post

Christian VasquezJuly 7, 2022

How To Create And Remove Directories in Linux With RMDIR and MKDIR (15)

Host your own blog with WordPress in Ubuntu

It’s 2022 and you don’t have a blog yet? It’s time to get to it! WordPress is an easy and great blogging ecosystem to jump

View Post

Christian VasquezMay 23, 2022

How To Create And Remove Directories in Linux With RMDIR and MKDIR (17)

Top 5 Best E-Mail Clients For Your Linux Machine

E-mail is always a fun topic to write about, and it never fails to provoke a fun debate. There are even arguments regarding how it

View Post

John BorrelliFebruary 6, 2022

You must be logged in to post a comment.

How To Create And Remove Directories in Linux With RMDIR and MKDIR (2024)
Top Articles
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6003

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.