Linux Commands

Linux rmdir Command Examples

The rm and rmdir commands in Linux are used to remove files and directories. Both are powerful commands and have very few command-line options. The difference between these commands is that rmdir only removes “empty directories,” and it does not remove files. If you use rmdir to remove a directory that contains files, it will fail with the message “Directory not empty”. If you need to remove a non-empty directory, use the rm command.

Another point is that when you remove a file or directory using rm and rmdir, it is instantly removed instead of moving towards Trash. Therefore, you will need to be careful while using these commands as you will not recover the removed files and directories unless you have a backup.

This post will describe how to use the rmdir command to remove directories in Linux and some examples.

Syntax of Rmdir Command

The syntax of rmdir command is as follows:

$ rmdir [OPTION]... DIRECTORY...

Remove a Directory using rmdir

To remove a single empty directory, type rmdir followed by the directory name or path to the directory as follows:

$ rmdir ~/Documents/myfiles

This command will remove the “myfiles” directory located in the “~/Documents” path only if it is empty. If the directory is not empty, the command will fail and displays the “Directory not empty” message.

Remove Multiple Directories using rmdir

To remove multiple empty directories, type rmdir followed by the directory names or path to directories as follows:

$ rmdir ~/Documents/myfiles images games

This command will remove the “~/Documents/myfiles”, “images”, and “games” directories only if they are empty.

Verbose output

To see what the rmdir command is doing in the background, use the -v option as follows:

$ rmdir -v ~/Documents/myfiles

For all the next commands, we will use the -v option.

Suppress Fail on Non-Empty Message

As discussed above, the rmdir does not remove the empty directories. Instead, it fails and displays a “Directory not empty” message. The –ignore-fail-on-non-empty option allows you to suppress the message. However, note that it still does not remove the non-empty directory.

Let’s say we want to remove three directories named “games”, “videos”, and “images” where “games” and “images” directories are empty while the “videos” directory is non-empty.

$ sudo rmdir -v games/ videos/ images/

You can see in the output, rmdir command has removed the games and images but failed to remove the videos directory and displayed the “Directory not empty” message.

If we use the –ignore-fail-on-non-empty option with the rmdir command, it will suppress the message; however, it will not remove the non-empty directory.

$ sudo rmdir -v --ignore-fail-on-non-empty games videos images

Remove Directory and its parent directories

The rmdir command also allows removing the directories along with their parent directories. Let’s say we have the following directory structure:

To remove the “docs” directory along with its parent directories “myfiles” and “files”, use the -p command as follows:

$ sudo rmdir –v -p files/myfiles/docs/

The Linux rmdir command is a part of the GNU Core Utilities. It allows you to remove empty directories in Linux. In this post, we covered how to use the rmdir command along with some examples. To view more information, view the rmdir man page.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.