The rmdir command in Linux is a built-in command-line utility that comes in handy when you need to remove directories. It is a GNU core utility that allows users to free up disk space by removing no longer-needed directories. In this article, I will describe all of the characteristics and applications of the rmdir command in Linux with numerous examples.
A. Description
The rmdir command in Linux is a useful command that is used to remove only “empty directories.” In addition, if you try to remove a non-empty directory using the rmdir command, it will prompt the “Directory not empty” error message. which prevents accidentally removing the non-empty directories and decreases the risk of losing necessary data.
B. Syntax
The syntax of the rmdir command in Linux is pretty simple, as shown below.
rmdir [OPTION]... DTRECTORY_NAME...
Note: In the syntax above, the OPTION enclosed by the square brackets means it is not mandatory and three dots after the square brackets and DTRECTORY_NAME mean multiple OPTIONS and DTRECTORY_NAMEs can be used after the rmdir command.
C. Options
There are numerous options available for the rmdir command. Here, I have listed a few of them. However, you can learn more about the rmdir command, its options and their uses by checking the man page.
man rmdir
Useful Options
- –ignore-fail-on-non-empty: Ignore each failure that is only because a directory is non-empty.
- -p, –parents: Remove the directory along with its empty content Directories.
- -v, –verbose: Generate a diagnostic report for each file that is processed.
- –help: Display this help Instruction.
- –version: Output version information.
Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.
Practical Examples of the “rmdir” Command in Linux
In Linux, the rmdir command is a helpful tool to remove empty directories. I will show you some of the most useful applications for the rmdir command in Linux using the directories in my Linux machine in the following section. In addition, the list of directories of my Linux machine is shown using the “tree” command which is as below.
Example 1: Remove an Empty Directory Using The “rmdir” Command in Linux
The “rmdir” command in Linux can be used to remove an empty directory. For example, I want to remove a directory in my machine named “Public.” For this, I will use the “rmdir” command in the steps mentioned below. You can remove any empty directory in your system by following the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ After that execute the below command, which will confirm the presence of the directory “Public”:
ls -l |grep Public
➌ Tap the ENTER key again.
➍ Then, run the following command to remove the directory “Public”:
rmdir Public
➎ After that, press the ENTER key.
➏ At this point, run the command below to check the current status of the directory “Public”:
ls -l |grep Public
➐ Finally, Press ENTER again.
Output >
In the following image, you can see that I have removed the directory named “Public.”
Similar Readings
- The “diff” Command in Linux [11 Practical Examples]
- The “cmp” Command in Linux [7 Practical Examples]
- The “comm” Command in Linux [10 Practical Examples]
Example 2: Remove Multiple Empty Directories Using the “rmdir” Command in Linux
To remove multiple empty directories using the “rmdir” command in Linux, you have to use the names of those directories as attributes of the command. These directories are eliminated from the filesystem in the order they are listed on the command line, that is, from left to right. For example, to remove the directories named “Music,” “Pictures,” and “Videos,” I can use the steps mentioned below.
Steps to Follow >
➊ First, open the Ubuntu Terminal.
➋ Next, run the command to ensure the presence of the directories “Music,” “Pictures,” and “Videos”:
ls
➌ Then, press the ENTER button.
➍ At this point, execute the command which will remove the directories named “Music,” “Pictures,” and “Videos.”
rmdir Music Pictures Videos
➎ After that, press the ENTER key
➏ Now, run the following command to check the current status of the desired directories.
ls
➐ Finally, tap the ENTER button.
Output >
In the image below, you can see that I have removed the directories named “Music,” “Pictures,” and “Videos.”
Example 3: Remove Empty Sub-Directories in Linux
The rmdir command allows you to remove any empty subdirectory by simply using the subdirectory’s path as an attribute to the command. In this section, I will remove a subdirectory under the “Document” directory named “day3”. You can remove any empty subdirectory by following the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Execute the command to ensure the existence of the subdirectory named “day3” under the “Document” directory.
tree Document
➌ Then, press the ENTER key.
The image below shows you that the day3 subdirectory is under the Document directory on my machine.➍ Now, run the following command to remove the day3 subdirectory.
rmdir Document/day3
➎ Now, hit the ENTER button.
➏ To see the new status of the “Document” directory, run the command below.
tree Document
➐ Finally, tap the ENTER button.
Output >
In the following image, you can see that I have removed the day3 subdirectory from the directory named “Document”.
Example 4: Remove a Non-empty Directory Using the “rmdir” Command in Linux
You can use the rmdir command to remove a non-empty directory that contains only one or more empty subdirectories. To remove the parent directory, you have to remove the subdirectories first. As I mentioned before, the rmdir command eliminates directories in the order they are listed on the command line, that is, from left to right. So, you can remove the subdirectories along with the parent directory with one command, following the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Then, execute the following command.
tree
➌ Press the ENTER button.
➍ Now, Run the command below to remove the subdirectories along with the parent directory.
rmdir Document/day1 Document/day2 Document
➎ After that, hit the ENTER key.
➏ Now, run the below command to check the current status of the “Document” directory.
tree
➐ Finally, Press the ENTER key.
Output >
The below image shows that I have removed the Document directory along with its subdirectories.
Similar Readings
- The “ln” Command in Linux [6 Practical Examples]
- The “lsof” Command in Linux [8 Practical Examples]
- The “tar” Command in Linux [12 Practical Examples]
Example 5: Remove an Empty Directory Along With Its Empty Parent Directories
The “-p” option with the “rmdir” command in Linux can be used to remove a directory and its parent directories if they are empty. This option is useful when you want to remove multiple empty directories at once instead of removing each one individually. I have a directory hierarchy structure of mydir1/subdir1/subdir11 as shown below. In This example I will remove “subdir11” and its parent directories “subdir1” and “mydir1”. If any of the parent directories are not empty, the rmdir command will fail to remove them. Now, I will remove “subdir11” along with its empty parent directories. You can do the same by following the steps below.
Steps to Follow >
➊ First, open the Ubuntu Terminal.
➋ Then, execute the following command.
rmdir -p mydir1/subdir1/subdir11
➌ Press the ENTER button.
➍ Now, Run the command below to check the current status of the “mydir1” directory.
tree
➎ After that, hit the ENTER key.
Output >
The below image shows that I have removed the subdir11 directory along with its empty parent directories.
Example 6: Use Regular Expressions With the “rmdir” Command
You can use “regular expression” i.e.’?’ and ‘*’ with the “rmdir” command in Linux. There is a directory named “Downloads” with three subdirectories named “Subdir1,” “Subdir2,” and “Subdir3” on my machine. In this example, I will remove the subdirectory under the “Downloads” directory using the ‘?’ expression. You can do the same by following the steps below.
Note: You will get the same result if you use ‘*’ instead of ‘?’.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Then, execute the command below.
tree
➌ Now, tap the ENTER button.
➍ At this point, execute the command to remove the desired subdirectories using ‘?’ with the rmdir command.
rmdir -v ~/Downloads/Subdir?
➎ Then, press the ENTER key.
Output >
In the following image, you can see that I have removed all directories that start with “Subdir” using ‘?’ with the “rmdir” command in Linux.
Note: In Linux, “?” and “*” have specific meanings. Where,
“?” (question mark) matches any single character.
“*” (asterisk) matches any sequence of characters.
Example 7: Remove the Directory in Verbose Mode Using the “rmdir” Command in Linux
To display the details of the execution of the “rmdir” command in Linux, you can use the “-v” option, which stands for “verbose.” This will display the details as the command is executed. Here, I will remove the ” Templates ” directory and display the execution details. You can get the same result by following the steps below.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Then, type the following command in the Ubuntu Terminal.
rmdir -v Templates
➌ Press the ENTER button.
Output >
In the following image, you can see that every change due to the execution of the “rmdir” command is displayed.
Conclusion
The rmdir command in Linux provides a simple and effective solution for removing directories. It is important to note that the directory must be empty before it can be removed. In this article, I’ve discussed the rmdir command and explained its options with some relevant examples. With this article, you should now have a better understanding of the rmdir command and how to use it. Hopefully, this article will make it easier for you to become skilled at using the Linux command line.
Similar Readings
- The “chgrp” Command in Linux [7 Practical Examples]
- The “file” Command in Linux [9+ Practical Examples]
- The “fsck” Command in Linux [7 Practical Examples]
- The “patch” Command in Linux [4 Practical Examples]
- The “stat” Command in Linux [9 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners