How to Remove or Delete a Directory in Linux

By 

Updated on

11 min read

Linux Remove (Delete) Directory

Removing a directory sounds simple until the directory is not empty, the name contains a space, or the terminal answers with “Directory not empty”. Linux offers several different methods for removing directories, and the right one depends on what the folder holds and how much confirmation you want along the way.

If you are using a desktop file manager such as Gnome’s Files or KDE’s Dolphin, you can delete a folder by locating it, right-clicking on it, and selecting 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 them from the command line.

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

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.

Warning
Deletion from the command line does not go through the Trash. Once a directory is removed with the commands explained in this article, it cannot be fully recovered. Read the path back to yourself before pressing Enter, especially when it comes from a variable or a glob pattern.

On most Linux filesystems, deleting a directory requires write and execute permission on its parent directory. Recursive deletion also requires enough permissions to access and remove the entries inside the tree.

Quick Reference

For a printable quick reference, see the rm cheatsheet .

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 empty directories below a starting directoryfind /dir -depth -mindepth 1 -type d -empty -delete
Remove a directory containing many filesrm -r -- /dir
Remove a directory whose name starts with a dashrm -r -- -dir
Remove a tree without crossing mount pointsrm -rf --one-file-system /dir

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. If one of the parents still holds other entries, rmdir stops at that level and prints an error.

In a cleanup script, that error is often expected rather than a problem. The --ignore-fail-on-non-empty option keeps rmdir quiet when a directory turns out to still have contents:

Terminal
rmdir -p --ignore-fail-on-non-empty parent/child/grandchild

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
Warning
rm -rf deletes without asking and without a way back. Never run it against /, and be careful with paths built from variables, because rm -rf $DIR/ becomes rm -rf / when $DIR is unset. GNU rm refuses to act on / by default, but that guard does not cover every variation, including rm -rf /*.

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

On a large tree it helps to see what actually went away. The -v (verbose) option prints each entry as it is removed:

Terminal
rm -rv dir1
output
removed 'dir1/notes.txt'
removed directory 'dir1/logs'
removed directory 'dir1'

The output shows notes.txt being removed first, followed by the empty logs directory and then dir1 after its contents are gone.

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.

If the tree you are clearing might contain a mount point, add the --one-file-system option. During recursive removal, rm then skips any directory that lives on a different filesystem than the path you passed on the command line, which stops a stray deletion from walking into a mounted backup drive or a network share:

Terminal
rm -rf --one-file-system /mnt/staging

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. Run the search on its own first. Without an action attached, find only prints what it matched, which is the cheapest way to catch a pattern that is wider than you intended:

Terminal
find . -type d -name '*_cache'

Once the list looks right, add the action that removes them. 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

Preview the empty directories with the same depth-first traversal that the deletion command will use:

Terminal
find /dir -depth -mindepth 1 -type d -empty -print

This preview shows directories that are empty before deletion. During the deletion pass, removing an empty child can make its parent empty as well, so additional parent directories may then match. The -mindepth 1 option still protects the starting directory.

If that scope is acceptable, add -delete as the final action:

Terminal
find /dir -depth -mindepth 1 -type d -empty -delete

Here is an explanation of the options used:

  • /dir - Recursively search in the /dir directory.
  • -depth - Process each directory’s contents before the directory itself.
  • -mindepth 1 - Keep the starting directory /dir out of the matches.
  • -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 include -depth in the preview so it follows the same traversal order as -delete, and use -delete as the last option.

Directory Names with Spaces or a Leading Dash

A directory whose name contains spaces has to be quoted, otherwise the shell splits it into separate arguments and rm looks for directories that do not exist:

Terminal
rm -r "my old backups"

Escaping each space with a backslash does the same job:

Terminal
rm -r my\ old\ backups

Names that begin with a dash cause a different kind of failure. Both rm and rmdir read -dir as a bundle of options and give up before touching anything. Passing -- marks the end of the options, so everything after it is treated as a path:

Terminal
rm -r -- -dir

Prefixing the path with ./ avoids the ambiguity just as well, and it is easier to remember in the middle of a cleanup:

Terminal
rm -r ./-dir

A symbolic link that points at a directory is still a link, not a directory, so it comes off with a plain rm and no -r:

Terminal
rm mylink

The target directory is left untouched. Where this goes wrong is the trailing slash. With a slash on the end, rm resolves the link to the directory behind it and refuses the request:

output
rm: cannot remove 'mylink/': Is a directory

Reaching for rm -rf mylink/ at that point is the dangerous move, because the slash sends the recursive delete through the link and into the contents of the target directory. Drop the slash instead. Our guide on removing symbolic links covers broken links and bulk cleanup.

Troubleshooting

/bin/rm: Argument list too long
This error usually appears when the shell expands a glob such as /dir/* into more filenames than the system can pass to one command. In that case, rm never starts.

If you want to remove the entire directory, pass the directory itself instead of expanding its contents:

Terminal
rm -r -- /dir

This gives rm one path to process recursively, so the number of entries inside /dir does not increase the shell’s argument list. The -- also prevents a dash-leading path from being read as an option.

Permission denied
Removing an empty directory requires write and execute permission on its parent directory. Recursive removal also requires enough permission to list the directory and remove the entries inside it. Check the ownership and modes on the path and its parent, then use sudo only when the path genuinely requires administrative access.

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 usually points to a sticky-bit rule, an immutable or append-only attribute, or a filesystem restriction rather than an ordinary open process. On a sticky directory such as /tmp, only the entry owner, the directory owner, or root can remove an entry. Check the parent with ls -ld and Linux file attributes with lsattr -d /dir before changing permissions or attributes.

Device or resource busy
On Linux, this usually means the directory is a mount point. Check it with findmnt --mountpoint /dir, then unmount it only if that is the intended action. On NFS, an open file that has been deleted can remain as a hidden .nfs* entry and report the same error. Use lsof +D /dir or fuser -vm /dir to find the process holding that entry open. An ordinary open file on a local filesystem does not normally block removal.

FAQ

How do I delete a folder in Linux?
Folder and directory mean the same thing on Linux. Use rmdir folder_name when the folder is empty, or rm -r folder_name when it has contents inside.

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?
Deleting an empty directory requires write and execute permission on its parent directory. Recursive deletion also requires enough permission to list the directory and remove its contents. Use sudo only when the path genuinely requires administrative access, or contact your administrator.

How do I delete only empty directories recursively?
Use find /path -depth -mindepth 1 -type d -empty -delete. This searches the tree depth-first, removes empty directories, and keeps the starting directory /path.

Conclusion

Preview directories with ls or find before adding -delete or running rm -rf; for recoverable desktop deletion, use gio trash <path> or trash-put instead of rm.

For more details, see our guides on the rm command and removing files and directories .

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