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:
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:
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:
This command will remove the “~/Documents/myfiles”, “images”, and “games” directories only if they are empty.
To see what the rmdir command is doing in the background, use the -v option as follows:
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.
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.
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:
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.