Removing Directories in Linux: A Easy Guide

Last updated: December 7, 2022 | Bobbin Zachariah

Learning how to utilize the Linux command line is crucial for all Linux users and system administrators. Simple maintenance activities like creating a file, browsing a folder, deleting a directory, or moving a file can all be done using it.

Removing files and directories in Linux using the graphical user interface is relatively easy. In this tutorial, we learn how to remove a directory in Linux using the terminal.

Remove a Directory in Linux

For this tutorial, I am using Ubuntu 22.04 but all the commands should work on any Linux distribution.

First, let us see the GUI approach to remove directories in Linux. Simply open any file explorer such as nautilus or dolphin.

And right-click on any folder you wish to delete/remove.

From Ubuntu GUI deleting a directory

You can either move it to trash or delete the directory permanently.

The below commands are used to remove files and directories in the Linux command line:

  • rmdir - The system's empty folders can be removed.
  • rm -r - Recursively remove the directory and all of its contents.

The previous commands are comparable to Windows' del and deltree functions which are also capable to remove multiple directories. Furthermore, it is important to remember that, unlike the GUI option, using the command line to delete files does not put the folder in the trash.

Removing directory using rm command

By default, rm is used to remove files. For removing a directory or directories you have to pass an option.

To remove non-empty directories with any content you will need to recursively delete them using the -r option.

Assuming in the Downloads directory, I have a folder named "SaMPlE" which contains some files. You can also list the contents of a particular folder using the ls command.

ls SaMPlE

Then remove the entire directory and its contents:

rm -r SaMPlE
removing non empty directory

It is important to use the -r option carefully, as it can permanently delete a large amount of data if used incorrectly.

Use the -d option to delete an empty directory. This helps you make sure the directory is empty before deletion and is identical to the rmdir command.

Let's cd into the directory where the directory is present using the following command

cd Downloads

Then list the contents of the folder using the ls command.

Finally, remove the empty directory

rm -d projects
removing empty directory using rm with -d option

Force Remove a Directory and its all files

The -r option tells rm to delete directories recursively, and the -f option tells it to force delete the files without prompting for confirmation.

rm -rf mydir

However, it is also a dangerous command because it can permanently delete a large amount of data without giving you a chance to confirm the action.

Removing empty directory using rmdir command

The rmdir command in Linux is a command-line utility that allows you to remove empty directories. It is similar to the rm command, but it only works on directories that are empty.

rmdir mydir

If there are files in them, the command will fail and return with a message. Let us see that with the help of below example :

I have a folder named "Tutorial" which is a non empty directory and contains some files. See what happens when I try to remove it using the rmdir command.

I get the following error message :

It clearly states that "failed to remove: Directory not empty". This means we can only delete empty directories in Linux using the rmdir command.

Remove Directory using find command

The find command-line tool can be used to look for files and directories and then take a certain action on the files and directories that match the search expression/pattern.

To remove a directory using the find command in Linux, you can use the -delete option to delete the directory and its contents.

The following command search for directories named with the suffix _db starting at the root directory (/) and delete any directories that are found:

find . -type d -name '*_db' -exec rm -r {} +

-type d : It will exclude files from the search and restrict to folders.
-name '*_db' : It only displays folders with the _db suffix.
-exec : Executes the external command rm -r.
-r : To delete multiple directories recursively.
{} + : Execute the least possible commands.


In summary, to remove a directory in Linux, you can use the rm -r command to recursively delete the directory and its contents. It is important to use this command carefully and to verify that you are removing the correct directory before using it.

Remember, the commands we mentioned simply remove the pointer to the filesystem (that means data still exists somewhere in the disk), If you looking to securely delete files use command such as shred.

Thanks for reading, please leave your feedback and suggestions in the below comment section.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment