rmdir command : How to delete a directory in Linux

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

To learn How to create a directory in Linux, see our guide:

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

First of all, let’s focus on some of the most important options that we can use with the rmdir.

OptionsExplanation
--ignore-fail-on-non-emptyIgnore each failure that is solely because a directory is non-empty
-p, --parentsRemove DIRECTORY and its ancestors
-v, --verbosePrint a message for each removed directory
--helpDisplay help page of rmdir Command
--versionCheck the version of rmdir command

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.

Example:

~$ 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/

5. Delete Parent directories using rmdir Command

To delete parent directories pass the -p option to rmdir command.

Now you must be wondering what is Parent Directory. Let me explain you with an example:

I want to delete a directory named data3 inside data1/data2/.

So data1 and data2 are parent directories of data3.

Parent Directories

So to delete the entire directory path i.e. data1/data2/data3 type the following command.

~$ rmdir -p data1/data2/data3/

If you run the above command without -p option, only data3 directory will be deleted.

To get the output message for each deleted directory type the following command.

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

You can also use the long option --parents.

~$ rmdir --parents data1/data2/data3

6. Solution : rmdir: failed to remove : Permission denied

The Linux operating system has some file systems in which you need root access to delete a file/directory.

For example, when I was deleting a directory inside the /var file system as a normal user, I received the following error.

~$ rmdir /var/mail/
rmdir: failed to remove '/var/mail/': Permission denied

To overcome this problem you have to run the command using sudo.

Example:

~$ sudo rmdir -v /var/mail/
[sudo] password for ubuntu:
rmdir: removing directory, '/var/mail/'

7. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of rmdir command.

~$ rmdir --help
~$ man rmdir

8. Check the version of rmdir Command

Check the rmdir command version using the following command.

~$ rmdir --version

Infographic

Refer to this Infographic for complete rmdir command options.

rmdir command

You can visit at following websites to get more information on rmdir command.

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of rmdir command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment