In this tutorial, we cover the basics of using the rmdir command on a Linux-based distribution.
The rmdir command is perfect if you want to prevent the accidental deletion of directories that contain files or directories. Whenever a directory is not empty, rmdir will throw an error and not proceed with removing the directory. If you still want to delete the directory, you will need to empty it first before proceeding to use rmdir.
There is a very limited use case for using rmdir, so I recommend using the rm command for most of your file and directory deletions. I would only use rmdir in cases where I do not want to accidentally delete directories that contain files.
You must ensure you have the correct file permissions before using the rmdir command to remove directories. If your user does not have sufficient permissions, you will see a permission denied error.
Table of Contents
rmdir Command Syntax
The syntax for the rmdir command is pretty straightforward. It accepts two arguments, with options being optional.
rmdir [OPTIONS] [DIRECTORY]
[OPTIONS] is where you specify any additional settings that you would like applied to the command. We cover the options you can use in the section below.
[DIRECTORY] is the directory you would like removed from the system.
rmdir Command Options
Below we cover the few options that you might want to use, but there aren’t many options that you can use with this command.
--ignore-fail-on-non-empty
will set rmdir not to throw an error whenever it fails to delete a directory because it is not empty.-p
or--parents
enables the deletion of directories parents if they are empty.-v
or--verbose
will output diagnostic information for every directory processed.
Using the rmdir command
The rmdir command is very basic, but it will come in handy when you want to delete directories and need to prevent accidentally deleting non-empty directories.
It is unlikely you will need to use any of the options available for this command, but we will quickly cover them for this guide anyway.
Basic rmdir Usage
To remove a directory using rmdir, simply write the command followed by the directory you wish to remove.
In our example, we will attempt to remove an empty directory named exampleDir
.
rmdir exampleDir
There will be no output on successful deletion unless you use the verbose -v
option. Instead, you can use the ls command to confirm the directory no longer exists.
dev@pimylifeup:~$ ls
testDir
Using Verbose
Below is an example of using the same command as above but with the -v
option.
dev@pimylifeup:~$ rmdir -v exampleDir
rmdir: removing directory, 'exampleDir'
Attempting to Delete a non-empty Directory
If you attempt to delete a directory that contains files and directories, rmdir
will throw an error message, and it will not proceed with deleting the directory.
dev@pimylifeup:~$ rmdir exampleDir
rmdir: failed to remove 'exampleDir': Directory not empty
Deleting Multiple Directories in a Single Command
You can delete multiple directories within a single command using rmdir. In our example below, we delete three directories within our current working directory.
rmdir exampleDir newDir testDir
We can check to see if they have all been deleted by using the ls
command.
dev@pimylifeup:~$ ls
dev@pimylifeup:~$
Delete Empty Parent Directories
By using the -p
option, you can delete any empty parent directories and your target directory. For example, using -p
with /exampleDir/subDir/testDir
will delete all three directories if they are empty.
dev@pimylifeup:~$ rmdir -p exampleDir/subDir/testDir
You can check that the base directory (exampleDir) has been deleted by using the ls
command.
dev@pimylifeup:~$ ls
testDir
Ignore non-empty Directories
You can suppress the warning output for when rmdir attempts to delete a directory that is not empty. To do this, simply use the --ignore-fail-on-non-empty
option.
Below is an example of suppressing the warning when trying to remove a non-empty directory. It is perfect if you want a quiet mode for this command.
rmdir --ignore-fail-on-non-empty exampleDir
The code example below is the output for when you disable the warning. We use the ls command to confirm that the directory still exists.
dev@pimylifeup:~$ rmdir --ignore-fail-on-non-empty exampleDir
dev@pimylifeup:~$ ls
exampleDir testDir
More Help
Like most Linux commands, there are a couple of ways to get more information within the operating system itself.
The first method is to use the man command. This command will bring up the manual pages for the specified command.
For example, below is how you can display the manual for the rmdir command.
man rmdir
Simply use the q
key to exit from the manual once you are done.
Alternatively, you can use the help option to display some information on the command and its options. To do this, simply enter rmdir followed by the --help
option.
rmdir --help
I hope that either of the options above helps you get the information you require.
Conclusion
I hope that you now understand how to use the rmdir command on Linux or Unix distributions. There is not a huge amount to cover for this command, so hopefully, you have picked up on all the basics.
If you plan on using Linux quite a bit, I highly recommend learning some of the other Linux commands that you can use. They will make life a lot easier for maintaining your chosen Linux-based operating system.
Please let us know if you notice a mistake or if an important topic is missing from this guide.