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

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.

					mkdir another/nested/dir -p -v

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.

└── another
    └── nested
        └── dir
            └── somefile.txt
rmdir */*/* -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.


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.

Meet the Author

Leave a Reply