The rmdir command is useful when you want to remove the empty directories from the filesystem in Linux. This command lets you specify the terminal to remove a particular directory right from the terminal. However, having correct knowledge of the rmdir command is essential, or you may end up deleting any important directory.
rmdir <options> <directory>
In the <options>, you can use various types of flags as per your requirements while removing <directory>.
rmdir Command in Linux With Examples
rmdir command is similar to the rm command, but rmdir only removes empty directories. So first, we will use the help flag to list down all the available options for the rmdir command:
The above command displays the various options such as:
- -p: This option removes the directory, including all its ancestors
- -v, –verbose: Displays verbose information for every directory.
- –ignore-fail-on-non-empty: This option does not report a failure that occurs because a directory is non-empty.
- –version: This option displays the version information and exit.
Example 1: The Basic rmdir Command
Let’s start the examples with a section with the simple rmdir command to remove multiple directories, and here is the basic syntax:
rmdir mydir1 mydir2 mydir3 .....
Here we will remove LINUX, INFO, and DETAIL directories through the following command:
rmdir LINUX INFO DETAIL
rmdir command in Linux
Example 2: The -p Option
You can use the -p option with the rmdir command to delete a directory, including all the subdirectories:
rmdir -p mydir1/mydir2/mydir3/...../mydirN
For example, we will delete the LINUX directory, including all its all ancestors, through the following command:
rmdir -p LINUX/mydir1/mydir2/mydir3
rmdir command with -p option
Example 3: The -v Option
If you want the terminal to display the message after removing the directory, you can use the -v option with the rmdir command:
rmdir -v dir1 dir2 dir3
Let’s now delete the LINUX, INFO, and DETAIL directories and display the message after their successful removal:
rmdir -v LINUX INFO DETAIL
rmdir: removing directory, 'LINUX'
rmdir: removing directory, 'INFO'
rmdir: removing directory, 'DETAIL'
rmdir command with -v option
Example 4: Remove Multiple Directories With the Same Expression
You can delete multiple directories if they have the same expressions by using the * in the rmdir command. For example, let’s remove all those directories which contain LINUX in their name:
rmdir -v LINUX*
rmdir: removing directory, 'LINUX1'
rmdir: removing directory, 'LINUX2'
rmdir: removing directory, 'LINUX3'
rmdir command to delete all files with the same expressions
In the above command, we have used the ls command to list all the available directories. Moreover, we executed the rmdir command -v option and * to delete all those directories which contain the same expression.
Example 5: The –ignore-fail-on-non-empty Option
Sometimes you get the following error while removing a directory through the rmdir command:
rmdir <option> <directory>
rmdir: failed to remove 'dir1': Directory not empty
So, in this case, you can use the –ignore-fail-on-non-empty to ignore the occurrences due to the non-empty directories. For instance, let’s remove the LINUX directory that contains sub-directories:
rmdir --ignore-fail-on-non-empty LINUX
rmdir command with –ignore-fail-on-non-empty option
This was a brief explanation of the rmdir command in Linux with examples. We have mentioned every option you can try while removing an empty directory from the terminal. If you are a beginner, you may receive errors while removing an empty or a non-empty directory.
That’s we have included the explanation on the –ignore-fail-on-non-empty option you can use if you get an error while using the rmdir command. Moreover, you can use multiple options of the rmdir command to remove the directories per the requirements.