Open In App

rmdir Command in Linux With Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In the world of Linux, managing directories is a fundamental aspect of system administration and everyday tasks. One of the essential commands for directory management is rmdir, which is used to remove directories. In this guide, we’ll explore the rmdir command in detail, covering its usage, options, examples, and execution.

Introduction to rmdir Command

The rmdir command in Linux is specifically designed to remove empty directories. Unlike the rm command, which can delete both files and directories, rmdir focuses solely on directories. It is a straightforward tool but nonetheless crucial for maintaining a tidy directory structure on your system.

Basic Syntax

The basic syntax of the rmdir command is:

rmdir [option] directory_name

Here, directory_name refers to the name of the directory you want to remove.

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:

rmdir --help

rmdir—help-option

The above command displays the various options such as:

Understanding Options

  • -p, --parents: This option enables the removal of parent directories as well if they become empty after removing the specified directory. It essentially removes the directory along with its parent directories if they become empty.
  • -v, --verbose: When used, this option displays a message for each directory processed, providing feedback on the action being performed.
  • --ignore-fail-on-non-empty: By default, rmdir fails if the directory is not empty. However, this option forces rmdir to ignore such failures and proceed with the removal.
  • –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:

ls 
LINUX1
LINUX2
LINUX3

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

Conclusion

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. 



Last Updated : 18 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads