How to Remove (Delete) Directory in Linux

By 

Updated on

7 min read

Linux Remove (Delete) Directory

Linux offers several different methods for removing directories. If you are using a desktop file manager such as Gnome’s Files or KDE’s Dolphin, you can quickly delete files and directories using the manager’s graphical user interface. Locate the file or directory you wish to remove, right-click on it, and select the “Delete” option. But, if you are working on a headless server or want to remove multiple directories at once, your best option is to delete the directories (folders) from the command line.

This article explains how to delete directories in Linux using the rmdir, rm, and find commands.

Quick Reference

TaskCommand
Remove an empty directoryrmdir dir1
Remove empty nested directoriesrmdir -p parent/child/grandchild
Remove a directory and its contentsrm -r dir1
Remove without confirmation promptsrm -rf dir1
Remove multiple directoriesrm -r dir1 dir2 dir3
Remove directories matching a patternfind . -type d -name '*_cache' -exec rm -r {} +
Remove all empty directoriesfind /dir -type d -empty -delete
Remove directory with too many filesfind /dir -type f -delete && rm -r /dir

Before You Begin

When you use a desktop file manager to delete a directory, it is actually moved to the Trash and can be easily recovered. However, be very cautious when deleting directories or files through the command line because once the directory is deleted using the commands explained in this article, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its contents. Otherwise, you will get an “Operation not permitted” error.

Directories with spaces in their names must be escaped with a backslash (\) character or wrapped in quotes.

Removing Directories with rmdir

rmdir is a command-line utility that enables you to delete empty directories. It comes in handy when you need to delete a directory, but you only want to do it if it is empty, without having to check its contents.

To delete a directory using rmdir, enter the command followed by the name of the directory you want to remove. For instance, if you want to delete a directory named dir1, you would type:

Terminal
rmdir dir1

If the directory is not empty, you will get the following error:

output
rmdir: failed to remove 'dir1': Directory not empty

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

To delete multiple empty directories at once, pass them as arguments:

Terminal
rmdir dir1 dir2 dir3

To remove a directory and its empty parent directories, use the -p option:

Terminal
rmdir -p parent/child/grandchild

This removes grandchild, then child, then parent, as long as each becomes empty after the nested directory is removed.

Removing Directories with rm

rm is a command-line utility for deleting files and directories. Unlike rmdir, the rm command allows you to delete both empty and non-empty directories.

By default, when used without any option, rm does not remove directories. To delete an empty directory, use the -d (--dir) option, and to delete a non-empty directory and all of its contents, use the -r (--recursive or -R) option.

For example, to delete a directory named dir1 along with all of its contents, you would type:

Terminal
rm -r dir1

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

Terminal
rm -rf dir1

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space:

Terminal
rm -r dir1 dir2 dir3

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. However, if the directory contains a large number of files, this can become tedious. In such cases, you can use the -I option, and rm will prompt you only once before proceeding with the deletion:

Terminal
rm -rI dir1

To remove the directory, type y and hit Enter:

output
rm: remove 1 argument recursively? y

You can also use glob patterns to match and delete multiple directories. For instance, to remove all first-level directories in the current directory that end with _bak, you would use the following command:

Terminal
rm -r *_bak

Using glob patterns when removing directories may be risky. It is recommended to use the ls command to list the directories before running the rm command, so you can see which directories will be deleted.

Removing Directories with find

find is a command-line utility that allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run:

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

Here is a breakdown of the command above:

  • . — Recursively search in the current working directory .
  • -type d — Restricts the search to directories.
  • -name '*_cache' — Search only directories that end with _cache.
  • -exec rm -r {} + — Executes rm -r on all matched directories.

Removing All Empty Directories

To remove all empty directories in a directory tree, you would run:

Terminal
find /dir -type d -empty -delete

Here is an explanation of the options used:

  • /dir — Recursively search in the /dir directory.
  • -type d — Restricts the search to directories.
  • -empty — Restricts the search only to empty directories.
  • -delete — Deletes all found empty directories in the subtree. The -delete option can only delete empty directories.

Use the -delete option with extreme caution. The find command line is evaluated as an expression, and if you add the -delete option first, the command will delete everything below the starting points you specified.

Always test the command first without the -delete option and use -delete as the last option.

Troubleshooting

/bin/rm: Argument list too long
This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on the size of the command line argument.

There are several different solutions to this problem. For example, you can cd to the directory and remove sub-directories one by one, either manually or using a loop .

The easiest solution is to first delete all files within the directory with the find command and then delete the directory:

Terminal
find /dir -type f -delete && rm -r /dir

Permission denied
You need write permission on the directory and its parent directory. Use sudo if appropriate or adjust ownership and permissions first.

Directory not empty (rmdir)
rmdir only removes empty directories. Use rm -r for non-empty directories or delete the contents first.

Operation not permitted
This can happen on protected system paths or when a directory is in use. Verify the path and make sure no process is using it, then retry with elevated privileges if needed.

FAQ

What is the difference between rmdir and rm -r?
rmdir only removes empty directories and will fail if the directory contains any files. rm -r removes directories and all of their contents recursively, including files and subdirectories.

How do I delete a directory that is not empty?
Use rm -r directory_name to delete a non-empty directory along with all its contents. Add the -f flag (rm -rf) to skip confirmation prompts for write-protected files.

Can I recover a directory deleted with rm?
No. Unlike the desktop file manager’s Trash, directories deleted with rm from the command line are permanently removed and cannot be easily recovered. Always double-check before running rm -rf.

Why do I get “Permission denied” when deleting a directory?
You need write permission on both the directory and its parent directory to delete it. Use sudo to run the command with root privileges if you own the system, or contact your administrator.

How do I delete only empty directories recursively?
Use find /path -type d -empty -delete. This searches the directory tree and removes only directories that contain no files or subdirectories.

Conclusion

With rm and find you can delete directories based on different criteria quickly and efficiently. Deleting directories is a simple process, but always double-check before running commands like rm -rf to avoid removing important data.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page