Removing a directory is meant to be straightforward, thanks to the Linux rmdir command. However, that’s not always the case. Many users need help removing a directory, especially if the directory is not empty.
Suppose you have the same issue; this post will hold your hand and guide you on the proper steps to follow. We will understand what the rmdir command does, the options it offers, and examples of removing directories, and we will address how to remove a non-empty Linux directory.
What is the rmdir Command?
The rmdir command is one of the Linux commands that you use when you want to remove directories. Although the Linux rm command also helps with removing directories, it mainly suits instances where the directory is not empty.
For the rmdir command, it is a perfect option for removing empty directories. It follows the syntax below.
$ rmdir [options] [directory]
The rmdir command offers a few options, which we’ve presented in the table below.
Option | Description |
-p | It removes a sub-directory and its parent directory. |
-v | It displays information about the rmdir process. |
–ignore-fail-on-non-empty | Suppress error message when removing a non-empty directory. |
–help | It shows the rmdir help page. |
–version | It shows the rmdir version. |
Examples of Using the rmdir Command to Remove Directories
We’ve given the syntax for the rmdir command and the various options you can use. However, it’s through practical examples that we can master how to work with the command.
But first, let’s create a few directories that we will use throughout this tutorial. We’ve used the Linux mkdir command to create the directories.
$ mkdir [directories]
Next, we run the ls command to verify the created directories and show the hierarchy for different sub-directory levels.
$ ls -R
rmdir Command Remove a Directory
Deleting an empty directory using the rmdir command is easy. You only need to add the name of your target directory, and it will be removed immediately.
$ rmdir names
We’ve removed the names directory and used the ls command to verify the deletion.
Removing Multiple Directories
When you have multiple directories, specify their names when running the rmdir command.
$ rmdir dir1 dir2
If the directories had the same naming format, you could use the asterisk (*) to match similar names and remove them at once.
rmdir Command with Verbose Mode
Adding the -v option will display information on the process happening when you run the command. For instance, when deleting a directory, we’ve added the -v, and we get information that the directory has been removed.
$ rmdir -v new/
Handling Non-Empty Directories
The rmdir command removes empty files. Thus, when you try removing a non-empty directory, you will get the error “failed to remove ‘dir’: Directory not empty.’
‘Linux remove directory not empty’ troubles many users, but we will discuss what solution to use.
Suppose we have the ‘Hello’ directory that contains a text file.
Running the rmdir command will raise the mentioned error.
There are two ways to handle such a case.
Option 1: Using the ‘ignore-fail-on-non-empty’ Option
The ignore-fail-on-non-empty option suppresses the error when you try to remove a non-empty directory.
Here’s an example.
$ rmdir --ignore-fail-on-non-empty Hello
However, this doesn’t mean that the directory gets deleted. Running the ls command shows that, although we didn’t get the error message, our directory still exists.
Option 2: Using the rm Command
The Linux rm command is the better option for removing a directory and its contents. Using the same directory in option 1 above, when we run the rm command, we will manage to delete. Hence, it is a better way instead of suppressing the error message.
$ rm -rv Hello/
Removing a Sub-directory and Its Parent
Still, on the rmdir command, you can add the -p option and the path to the sub-directory you want to remove. This way, you will also remove the parent directory.
Here’s an example.
$ rmdir -v -p Hosts/users/level1/
Conclusion
The rmdir command helps with removing empty directories. We’ve discussed the rmdir command in detail, giving examples of how to use it under different circumstances. Moreover, we’ve discussed what it takes to remove a non-empty directory.