Skip to main content

Directory Operation Commands

rmdir Command Examples

Learn everything about the rmdir command in Linux which is used for deleting folders in Linux command line.

rmdir is one of those basic Linux commands that you'll learn in the beginning but hardly use.

Why? Because it may sound like rmdir is used for removing directories (i.e., folders), the truth is that it can only delete the empty directories.

Confused? Don't be. Let's see it in detail.

Using rmdir command in Linux

The rmdir has a simple syntax:

rmdir [option] directory

Let's see its usage.

Delete an empty directory

An empty directory contains no files or directories in it. If you have an empty directory, it can be easily removed. No other options are needed.

rmdir directory_name
Deleting empty directory in Linux

Ignoring the warning while deleting non-empty directory

If you try to delete a directory that contains files and directories, the command will fail with an error message indicating 'Directory not empty'.

You can suppress this warning in the following way:

rmdir --ignore-fail-on-non-empty directory_name
Deleting non empty directory in Linux with rmdir command

While it no longer shows the error message, the directory is still not deleted. To delete a folder in Linux terminal, you can use the rm command in the following manner:

rm -r directory_name

Delete directory along with parent directory

If you have a nested directory structure, you can specify the option -p to delete the complete structure.

So if you have directory structure a/b/c, you can use:

rmdir -p a/b/c

It is similar to rmdir a/b/c a/b a.

Do note that the directories need to be empty as well.

Verbose mode

There is also a verbose mode that you may use. You might have already noticed that the rmdir command doesn't show any result for successful commands.

If you want to see that, you can use the verbose option -v:

rmdir -v directory
Removing directory in verbose mode

And that's all about the rmdir command. Since most of the time you'll be folders that are not empty, rmdir will fail to delete them. The --ignore-fail-on-non-empty is not something many people remember, and this is why the rm -r command is more popular.

Abhishek Prakash