5 Practical Examples To Delete / Remove Directory in Linux ( rmdir )

Question: How can I delete empty directory, directory with files and sub directories in Linux / Unix ? Also, how can I use an alias effectively for rm and rmdir command?
Answer: You can delete empty directory using rmdir command, or directory with content using rm command. Deletion can be done interactively, recursively, forcefully, or through alias. In this article, let us review 5 practical examples that shows how to delete the directories in Linux like systems.

1. How to Delete Empty Directories in Unix?

rmdir command will delete the empty directories. i.e directory without any sub-directories or files.


To ensure that you are deleting an empty directory you should use rmdir command. If there is any
files / directories in that directory it will display the following error.

$ rmdir test
rmdir: failed to remove `test': Directory not empty

2. How to Delete Nested Empty Directories in Linux?

Use option -p, to delete nested directories as shown below.

$ rmdir -p dir1/dir2/dir3

Note: Don’t get panic that how a directory can be nested and also empty. It is nested when you are invoking the command, but it deletes the inner most directory first, and makes the next level directory empty then it deletes that directory. And it continue doing so.

The rmdir -p dir1/dir2/dir3 is equivalent to

$ rmdir dir1/dir2/dir3 dir1/dir2 dir1

3. Delete Directory Which has Content (i.e Directory with Files and Sub-directories)

Some times you may want to delete directory which has contents in it. You can do it with rm command as shown below.

$ rm -rf DIRNAME

This will delete the directory including all the files and sub-directories. This is very dangerous when you use it accidentally as you cannot recover those files easily. So it is strongly recommended that you pay attention and think twice before executing the rm -rf command.

4. Delete Interactively: Avoid using -f in rm at the early stages.

If you’ve ever accidentally executed rm -rf by mistake, you may want to seriously consider using -i option to delete the files and folders interactively as shown below (especially under root).

Deleting a directory recursively & interactively.

# rm -ir DIRNAME

Deleting a file interactively.

# rm -i FILENAME

If you are a Linux newbie, don’t use -f option in root until get super comfortable with the command line. Instead, try to use -i option as shown above.

5. Useful rm and rmdir Aliases

You can make the interactive rm option as your default rm command using alias as shown below.

Alias to make rm interactive

# alias rm="rm -i"

While using the rm command, it will be always execute rm -i and ask for confirmation before deleting any files. But be careful that when you give -f option then you wont get the interactive prompt even if you have the -i option in it.

For example, in the following command -i don’t have any effect.

$ rm -irf DIRNAME

In this case, the above command (with the rm alias) is equivalent to the following command.

$ rm -rf DIRNAME

All the following rm options are valid and does the same functionality. i.e recursive and forceful delete.

  • rm -fr
  • rm -rf
  • rm -r -f
  • rm -f -r

Alias to make rm verbose

If you want rm or rmdir to print what it is doing then you can use the traditional verbose option.

alias rm="rm -v"
alias rmdir="rmdir -v"

Note: Make this setting permanent by adding alias command to either ~/.bash_profile (or) ~/.bashrc file.

You can remove files or directories, empty or non-empty, nested or single using rm and rmdir commands. But before invoking the command think twice about deleting as it will become hard to recover a file once you’ve deleted it by mistake.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: