Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
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.
Syntax
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.
Options
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.
Examples of rmdir command in Linux
Let us discuss a few examples to get a better understanding of the rmdir command.
Example #1
rmdir sampledir1
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.
Example #2
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.
Example #3
rmdirdir/subdirect dir
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.
Example #4
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.
Syntax:
$ 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
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
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.