The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (2024)

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The rmdircommand in Linuxis a built-in command-line utilitythat comes in handy when you need toremovedirectories. 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 thermdircommand in Linuxwith numerous examples.

Table of Contents

A.Description

The rmdircommand inLinuxis a useful command that is used to removeonly“empty directories.”In addition, if you try to removeanon-emptydirectory using thermdir command, it will prompt the “Directory not empty” error message. which prevents accidentally removing the non-emptydirectories and decreases therisk of losing necessary data.

B.Syntax

The syntax of thermdircommand in Linuxis 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_NAMEmean multiple OPTIONS and DTRECTORY_NAMEs can be used after the rmdir command.

C.Options

There are numerous optionsavailable for the rmdircommand. Here, I have listed a few of them. However, you can learn more about the rmdir command, its optionsand their uses by checking the manpage.

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 optionsare 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 Linuxusing the directories in myLinux machine in the following section. In addition, the list of directories of my Linuxmachine is shown using the “tree” command which is as below.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (2)

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 canremoveany empty directoryin 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 theENTER key again.

➍ Then, run the following command to remove the directory “Public”:

rmdir Public

➎ After that, press the ENTERkey.

➏ At this point, run the command below to check the current status of the directory “Public”:

ls -l |grep Public

➐ Finally, Press ENTERagain.

Output >

In the following image, you can see that I have removedthe directory named “Public.”The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (3)

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 thenames of those directories as attributes of the command. These directories areeliminated from thefilesystem 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 ENTERbutton.

➍ At this point, execute the command which will remove the directories named “Music,”“Pictures,”and “Videos.”

rmdir Music Pictures Videos

➎ After that, press the ENTERkey

➏ Now, run the following command to check the current status of the desired directories.

ls

➐ Finally, tap the ENTERbutton.

Output >

In the image below, you can see that I have removed the directories named “Music,”“Pictures,”and “Videos.”The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (4)

Example 3: Remove Empty Sub-Directories in Linux

The rmdir command allows you to remove any empty subdirectoryby simply using the subdirectory’s pathas 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 ENTERkey.

The image below shows you that the day3 subdirectory is under the Document directory on my machine.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (5)➍ Now, run the following command to remove theday3subdirectory.

rmdir Document/day3

➎ Now, hit the ENTERbutton.

➏ To see the new status of the “Document”directory, run the command below.

tree Document

➐ Finally, tap the ENTERbutton.

Output >

In the following image, you can see that I have removed the day3 subdirectory from the directory named “Document”.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (6)

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 rmdircommandeliminatesdirectories 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 ENTERbutton.

➍ Now, Run the command below to remove the subdirectories along with the parent directory.

rmdir Document/day1 Document/day2 Document

➎ After that, hit theENTER key.

➏ Now, run the below command to check the current status of the “Document”directory.

tree

➐ Finally, Press the ENTERkey.

Output >

The below image shows that I have removed the Documentdirectory along with its subdirectories.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (7)

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” optionwith the “rmdir” command in Linux can be used to remove a directoryand itsparent directories if they are empty. This option is useful when you want to remove multiple emptydirectories at once insteadof removing each one individually. I have a directory hierarchy structure of mydir1/subdir1/subdir11 asshown below.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (8) In This example I will remove “subdir11”and its parent directories“subdir1”and “mydir1”. If any of theparent directories are not empty, thermdir 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 ENTERbutton.

➍ Now, Run the command below to check the current status of the “mydir1”directory.

tree

➎ After that, hit theENTER key.

Output >

The below image shows that I have removed the subdir11directory along with its empty parent directories.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (9)

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 ENTERbutton.

➍ At this point, execute the command to remove the desired subdirectories using ‘?’ with the rmdircommand.

rmdir -v ~/Downloads/Subdir?

➎ Then, press theENTER 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.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (10)

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 detailsof the execution of the“rmdir”command inLinux,you can use the “-v” option, which stands for“verbose.” This will display thedetails 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 ENTERbutton.

Output >

In the following image, you can see that every change due to the execution of the“rmdir” command is displayed.The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (11)

Conclusion

The rmdircommand in Linuxprovides a simple and effective solution for removing directories. It is important to note that the directory must be emptybefore it can be removed. In this article, I’ve discussed the rmdir command and explained its optionswith some relevant examples. With this article, you should now have a better understanding of the rmdircommand 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]

Rate this post

The “rmdir” Command in Linux [7 Practical Examples] - LinuxSimply (2024)

References

Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6222

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.