How to Delete Empty Directories Using the rmdir Command

rmdir-command-linux

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.

The rmdir command offers a few options, which we’ve presented in the table below.

OptionDescription
-pIt removes a sub-directory and its parent directory.
-vIt displays information about the rmdir process.
–ignore-fail-on-non-emptySuppress error message when removing a non-empty directory.
–helpIt shows the rmdir help page.
–versionIt 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.

linux-mkdir-command-create-directory

Next, we run the ls command to verify the created directories and show the hierarchy for different sub-directory levels.

linux-ls-command-hierarchy-of-directory

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.

We’ve removed the names directory and used the ls command to verify the deletion.

rmdir-command-remove-directory

Removing Multiple Directories

When you have multiple directories, specify their names when running the rmdir command.

rmdir-command-remove-directories

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-command-verbose-mode

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.

mkdir-command-create-non-empty-directory

Running the rmdir command will raise the mentioned error.

rmdir-command-error-deleting-non-empty-directory

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.

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.

rmdir-ignore-fail-on-non-empty-directory

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-command-remove-directory

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-command-remove-sub-directories

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.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top