Skip to main content

Module 1: File & Directory Management

Lesson 9: rmdir Command

In this lesson, you'll learn how to use the rmdir command to remove empty directories in Linux.

As the name suggests, the rmdir command is used to remove the directory. However, it is important to note that it can remove empty directories only.

The syntax of the rmdir command is similar to other Linux commands. At a high level, it is divided into two parts - options and arguments:

rmdir [OPTIONS] ... <DIRECTORY1> <DIRECTORY2> ...

Here, the square brackets ([]) represent the optional arguments, whereas angular brackets (<>) represent the mandatory arguments.

Let's discuss the basic usage of the rmdir command.

rmdir Command Options

Option Description
-p Remove the directory and all its empty ancestor directories
-v Verbose mode - print a diagnostic for every directory processed
--ignore-fail-on-non-empty Ignore failures caused by non-empty directories
--help Display help information and exit
--version Output version information and exit

1. Delete an Empty Directory in Linux

First, create a few empty directories:

$ mkdir dir1 dir2 dir3 dir4

Let's verify that the required directories have been created:

$ ls -l
total 0
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir1
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir2
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir3
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir4

Now, let's remove the dir1 directory and verify that it has been removed:

$ rmdir dir1
$ ls -l
total 0
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir2
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir3
drwxr-xr-x 2 tecmint tecmint 40 Nov  5 10:20 dir4

In a similar fashion, we can use the rmdir command to remove multiple empty directories at once.

Let's remove the remaining directories:

$ rmdir dir2 dir3 dir4

Finally, verify that all the directories have been removed:

$ ls -l
total 0

Here, we can see that the ls command doesn't show any directory.

2. rmdir Verbose Mode