Linux rmdir Command Examples

The “rmdir” stands for “remove directory” command utility that removes the empty directories in the filesystem of Linux. It is pre-installed in almost all Linux distributions. It does not delete any directory that contains some files or directories. To the successful execution of the “rmdir” command, the target directory must be empty.

This post provides a detailed view of the “rmdir” command and its objective, working, and usage. The content list that is carried out for this post of this post is written below:

How Does the rmdir Command Work?

The working of the “rmdir” command relies on its syntax. The generalized syntax of the “rmdir” command is specified below.

Syntax:

$ rmdir [option] [directory]

The “rmdir” syntax contains the following components:

  • rmdir:  Main keyword that represents the “rmdir” command.
  • option: Shows the essential supported option of the “rmdir” command.
  • directory: Denotes the target directory that would be removed

Type the “help” command to get a detailed view of the “rmdir” tool along with its supported options:

$ rmdir --help

Now, move on to the various practical examples to check the working of the “rmdir” command.

Example 1: Remove a Single Empty Directory

The main objective of the “rmdir” command is to delete the empty directory available in the system. Suppose there is an “EmpDir” empty directory located in the “home” directory:

$ ls -l EmpDir

Simply execute the “rmdir” command to delete the “EmPDir” in the following way:

$ rmdir EmpDir

The output confirms that the “EmpDir” has been removed from the local system.

Remove Multiples Empty Directories

The “rmdir” command can also delete multiple empty directories simultaneously. For this purpose, it follows the below-mentioned syntax:

$ rmdir [directory1][directory2]...........[directoryN]

As an example, four empty directories “Dir1”, “Dir2”, “Dir3”, and “Dir4” are removed using the above “rmdir” syntax:

$ rmdir -v Dir1 Dir2 Dir3 Dir4

In the above command “-v(verbose mode)” flag is used to display the information of the performed action.

Example 2: Remove the Sub-Empty Directory Structure

The “-p” option of the “rmdir” command is beneficial for deleting the structure of the empty directories, like a child to the parent directory. In this case, the “ParentDir” directory is created having the structure like this:

$ mkdir -p ParentDir/subdir1/subdir2/subdir3/subdir4

Type the “rmdir” command by following the combination of “-p” and “-v” argument and the directory structure in this way:

$ rmdir -pv ParentDir/subdir1/subdir2/subdir3/subdir4

The output verifies that all the empty subdirectories and the “ParentDir” have been removed successfully.

Example 3: Remove Empty Directories Using Wildcard

Like other Linux commands, the “rmdir” command also accepts the wildcard “*” as an argument.

In this example, the “Documents” directory contains seven empty directories by following the same prefix at the start of their name i.e. “dir”:

$ ls -l

Now, execute the “rmdir” command with its argument “-v” and the wildcard “*” followed by the prefix “dir”:

$ rmdir -v dir*

Here all the empty directories start with the prefix “dir” has been removed.

Conclusion

Linux provides a built-in command line tool, the “rmdir” command to remove the empty directories and subdirectories from the local system. This command does not remove the directory until it is fully empty. It also provides a limited list of supported options for special tasks. This post has briefly illustrated the basics, working, and usage of the “rmdir” Linux command.