So we’ve already discussed the rm command that’s primarily used for deleting files and directories from the Linux command line. However, there’s another, related command line utility that is specifically aimed at removing directories. The tool in question is rmdir, and in this tutorial, we will discuss the basics of it using some easy to understand examples.
Linux rmdir command
As the name suggests, the rmdir command is focused at removing directories, although empty-ones only. Following is its syntax:
rmdir [OPTION]… DIRECTORY…
And here’s how the man page explains it:
Remove the DIRECTORY(ies), if they are empty.
The following Q&A-styled examples should give you a good idea on how this utility works.
Q1. How rmdir works?
That’s pretty straight forward – just pass the directory name as input to the command. For example:
Q2. How to make rmdir ignore non-empty directories.
BY default, the rmdir command throws an error if you try deleting a non-empty directory. However, if you want, you can suppress this behavior of rmdir using the –ignore-fail-on-non-empty option.
Q3. How to make rmdir remove parent directories as well?
Just like in the case of mkdir, you can also ask rmdir to perform its operation on parent directories. What that means is, you can also delete parent directories of a directory in one go. This feature is accessible through the -p command line option.
For example, the following command will delete both ‘test’ and ‘test-dir’ directories.
rmdir -p test/test-dir/
Note: For this operation to work, all parent directories should not contain anything other than the empty-directory being deleted.
Q4. What is the difference between rmdir and rm -r ?
If you remember, you can also delete directories using the rm command by enabling the -r option it provides. So what’s the difference between that and rmdir? Well, the answer is rmdir only works in the case of empty directories – there’s no way whatsoever you can use to make rmdir delete non-empty directories.
So rmdir is a useful in tool in those situations where you otherwise need to check if a directory is empty before deleting it.
As you’ll agree, rmdir isn’t a complex command to understand and use. Plus, it offers only a handful command line options. We’ve discussed almost all of them here, so practice the examples mentioned in this article, and you should be good to go. Just in case you need, here’s the man page for rmdir.
Share this page: