rmdir command : How to delete a directory in Linux

Today we will learn how to delete (Remove) a empty directory in Linux operating system using rmdir command.

Let’s get started.

The rmdir command is derived from an ordinary word and that is Remove Directory.

What we call a folder in Microsoft Windows is called a directory in Linux.

Features of rmdir Command

  • Delete (Remove) a directory
  • Delete Parent Directories
  • Display Output Message

You must follow the syntax given below to use the rmdir command.

rmdir [OPTION]... DIRECTORY...

1. Delete a empty Directory

To delete a empty directory type the following command.

~$ rmdir data/

2. Delete Multiple empty Directories

To delete multiple empty directories, pass the names of the directories to rmdir command.

In this example, I am deleting three directories named data1, data2, and data3.

~$ rmdir data1/ data2/ data3/

There are other methods by which we can delete multiple directories.

Method #1

You can specify the names of directories inside a curly bracket. Make sure that the directory names should be separated by commas.


~$ rmdir {data1,data2,data3}

Method #2

You can also use the following method to delete multiple directories.

~$ rmdir data{1,2,3}

3. Ignore Fail on Non-Empty Directories

As we saw, rmdir can only delete empty directories.

And when we delete a non-empty directory, we get the following error message.

~$ rmdir data/
rmdir: failed to remove 'data/': Directory not empty

Using the --ignore-fail-on-non-empty option with rmdir command, you can ignore error messages of non-empty directories.

~$ rmdir --ignore-fail-on-non-empty data/

Note: The above command will not remove the directory, but will stop displaying error message.

4. Display Output Message for each Deleted(Removed) Directory

To display the output message for each deleted directory pass the -v option to rmdir.

Let’s take some examples:

Task #1 Remove a directory named data.

~$ rmdir -v data/
rmdir: removing directory, 'data/'

Task #2 Remove multiple directories named data1, data2, and data3.

~$ rmdir -v data1/ data2/ data3/
rmdir: removing directory, 'data1/'
rmdir: removing directory, 'data2/'
rmdir: removing directory, 'data3/'

You can also use the long option --verbose.

~$ rmdir --verbose data/

Read Complete Story