In this Linux quick tip we will examine how to delete a directory in Linux from the command line. You can delete a directory using the rmdir command. However, it is not always straight forward.

The rmdir command is part of the GNU Core Utilities. The rmdir command's man page specifically calls it a utility to "remove empty directories". This why people new to Linux command line run into a lot of problems when trying to delete a non-empty directory. In this Linux quick tip we will discuss deleting directories, and working around the "Directory is not empty" error message.

Delete a Directory Using rmdir

A directory can be deleted from the Linux command line quite easily. Call the rmdir utility and pass the name of the directory as an argument.

rmdir cats
[mcherisi@putor animals]$ ls -rlt
total 12
drwxrwxr-x. 2 mcherisi mcherisi 4096 Dec 23 10:40 dogs
drwxrwxr-x. 2 mcherisi mcherisi 4096 Dec 23 10:40 cats
drwxrwxr-x. 2 mcherisi mcherisi 4096 Dec 23 10:41 birds

[mcherisi@putor animals]$ rmdir cats

[mcherisi@putor animals]$ ls -lrt
total 8
drwxrwxr-x. 2 mcherisi mcherisi 4096 Dec 23 10:40 dogs
drwxrwxr-x. 2 mcherisi mcherisi 4096 Dec 23 10:41 bird

This works great if the directory is empty. If there is something inside the directory, it will fail and print the following warning:

[mcherisi@putor animals]$ rmdir dogs
rmdir: failed to remove 'dogs': Directory not empty

This is a built in warning to let you know the directory is not empty. This saves you from inadvertently deleting files. Let's examine some ways to work around the rmdir: failed to remove 'xxxx': Directory not empty error.

Deleting a non-empty Directory in Linux

There are several ways to delete a directory and it's contents.

The safest way to do this is to cd (change directory) into the directory to be deleted, and use the rm command to delete the files.

[mcherisi@putor animals]$ cd dogs
[mcherisi@putor dogs]$ rm *
[mcherisi@putor dogs]$ cd ..
[mcherisi@putor animals]$ rmdir dogs

NOTE: If there is non-empty sub-directories, you will receive the same "directory not empty" warning.

This allows you to look at the files you are deleting. However, if you are confident that you want to delete the directory and all of it's content (including sub-directories), you can use the rm command directly.

In order to delete a directory and it's contents with the rm command, you must use a special option. If you simply try to delete the directory with rm, it will show you the following warning.

[mcherisi@putor animals]$ rm dogs
rm: cannot remove 'dogs': Is a directory

You must use the recursive option with rm. Here we will use the recursive (-r) option to delete the directory and all of it's contents.

[mcherisi@putor animals]$ rm -rv dogs
removed 'dogs/bulldog'
removed 'dogs/poddle'
removed 'dogs/pugs'
removed directory 'd

NOTE: We used -r (recursion) is conjunction with -v (verbose) so we can show what was being deleted. See "rm Command - Deleting Files on the Linux Command Line" for more information.

The rmdir Command Options

The rmdir command does not provide a lot of additional options. Let's take a look at the few that exist and what they do.

Verbose Output

Using the -v (--verbose) option will display output for every directory processed by the rmdir command.

[mcherisi@putor animals]$ rmdir -v cats
rmdir: removing directory, 'cats'

Ignore Fail on Non Empty

At first you might think this option allows you to delete non-empty directories. However, it simply suppresses the warning message.

[mcherisi@putor animals]$ ls dogs/
bulldog  poddle  pugs
[mcherisi@putor animals]$ rmdir --ignore-fail-on-non-empty dogs
[mcherisi@putor animals]$ ls dogs/
bulldog  poddle  pugs

As you can see, rmdir did not display the "Directory not empty" warning. It also did not delete the directory. Again, this option simply suppresses the warning message.

Remove Directory and It's Parents

The -p (--parents) options allows you to delete a directory and it's parents. For example, let's say you had the following directory structure:

fish
└── cichlids
    └── african

Using the -p option to delete the "african" directory, you can also delete it's parents (cichlids and fish).

[mcherisi@putor animals]$ rmdir -pv fish/cichlids/african/
rmdir: removing directory, 'fish/cichlids/african/'
rmdir: removing directory, 'fish/cichlids'
rmdir: removing directory, 'fish

NOTE: We use the -v (--verbose) option to show what was being deleted.

Conclusion

Deleting a directory in Linux is easy once you understand how the rmdir and rm commands function. In this article we covered how to delete directories, how to delete non-empty directories, and all of the options available to the rmdir command. Feel free to leave any questions or comments below.

Resource and Links