Introduction to rmdir command in Linux
We know that the Linux system is made up of files and directories. There may be plenty of directories in our system which is no longer needed. So we need to clean them up periodically. In Linux, there is a command called rmdir which is used to remove directories from our Linux file system. In this article, we will discuss how to use the rmdir command with various command line options.
rmdir Command Description
The rmdir command is used to remove empty directories from our Linux Operating System. Every directory that needs to be removed should not contain any files or subdirectories. Otherwise, the rmdir command cannot remove the directory from our file system.
If the directory specified is not empty, then the rmdir command will not remove the directory and it will try to remove other specified directories in the command. The directories are removed in the order that they are specified in the command line i.e. from left to right.
To remove both the parent and subdirectory of the parent, the subdirectory should be mentioned first so that the parent directory is made empty and rmdir can remove it. The rmdir command is somewhat similar to rm -d command. If you desire to remove a non-empty directory, then you can use rm command with the recursive option.
The syntax of the rmdir command is given below.
rmdir [-p] [-v, --verbose] [--ignore-fail-on-non-empty] [ directory names....]
Here p, v, and ignore-fail-on-non-empty are options used in the rmdir command.
There are plenty of options that can be used with the rmdir command. Let us discuss them one by one.
- -help: This will display the syntax of the rmdir command along with the options. It will also provide a brief explanation about each option.
- -p option: This option will remove the specified directory and its parent directories. In this option every specified directory name will be considered as the pathname of which all directories will be removed beginning from the last component.
- –ignore-fail-on-non-empty: Usually when you try to remove a directory which is not empty using the rmdir command, it will produce an error message. This option will not report a failure message which occurs purely because of a non-empty directory.
- -v, –verbose: This option displays verbose information of each directory that is being processed.
- -v, –version: This option will print the information of the version and exit.
Examples of rmdir command in Linux
Let us discuss a few examples to get a better understanding of the rmdir command.
The above command will remove the directory named sampledir1 if it is empty. If it is not empty then the error message will be produced.
In this case, you have to remove the directory contents before deleting it.
To remove multiple directories at once use the following command.
rmdir sample1 sample2 sample3
The above command will remove all the directories sample1 sample2 and sample 3 if they are empty. If any of the directories is not empty then an error message will be displayed for that particular directory and other directories will be removed from the file system.
The above command will remove the directory named dir/subdirect if the directory is empty and then directory dir will be removed if it is empty after dir/subdirect is removed.
rmdir -p dir/subdirect
It is the same as the command shown in example 3. rmdir command will remove the directory dir/subdirect and then it will remove the dir directory.
How to Ignore Non-Empty Directories?
By default, if you are trying to remove a non-empty directory, the rmdir command will produce an error message. To ignore the error message that arises on deleting a non-empty directory, use the –ignore-fail-on-non-empty option.
$ rmdir--ignore-fail-on-non-empty test-dir
How to Remove Parent Directories Using rmdir command?
rmdir command can also perform its operation over the parent directories. It has the capacity to delete both the directory and its parent directories in one shot. You can make use of this feature with the help of-p option.
Consider the following example.
$ rmdir -p test/testdir/
The above command will delete both test and testdir directories. For this to happen, all parent directories should be empty other than the empty directory to be deleted.
How to Get Help for rmdir command
To get help about the rmdir command uses the following command.
$ rmdir --help
The following output is produced displaying a detailed description of the rmdir command.
You can also use the man command to display the manual pages of the rmdir command.
$ man rmdir
How to Display the Version Information of rmdir command?
To display the version information, use the –version option along with rmdir command as shown below.
$ rmdir --version
The following output is produced.
Difference Between rmdir and rm –r
You can also use the rm command to delete directories by using the -r option alongside the command. So what is the basic difference between both the commands? Well, the answer is simple. The rm command can be used to delete non-empty directories as well but rmdir command is used to delete only empty directories. There is absolutely no way to delete non-empty directories using the rmdir command.
Advantages of the Rmdir Command
- rmdir command deletes only empty directories which helps us to avoid the accidental loss of our data. This is an important feature because in the Linux system once the data is deleted it is extremely difficult to recover the data.
- It can delete multiple directories at once.
- The rmdir command is simple and easy to use both by beginners and experts.
This is a guide to rmdir command in Linux. Here we also discuss the introduction and how to get help for rmdir command along with an example and its code implementation. You may also have a look at the following articles to learn more –