How to Remove or Delete a Directory in Linux

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 Files or KDE’s Dolphin, right-click the folder and choose Move to Trash. 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
You can restore a directory while it is still in the Trash. The file manager’s permanent delete action bypasses the Trash.
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 .
| Task | Command |
|---|---|
| Remove an empty directory | rmdir dir1 |
| Remove empty nested directories | rmdir -p parent/child/grandchild |
| Remove a directory and its contents | rm -r dir1 |
| Remove without confirmation prompts | rm -rf dir1 |
| Remove multiple directories | rm -r dir1 dir2 dir3 |
| Remove directories matching a pattern | find . -type d -name '*_cache' -prune -exec rm -r {} + |
| Remove empty directories below a starting directory | find /dir -depth -mindepth 1 -type d -empty -delete |
| Remove a directory containing many files | rm -r -- /dir |
| Remove a directory whose name starts with a dash | rm -r -- -dir |
| Skip nested directories on other filesystems | rm -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:
rmdir dir1If the directory is not empty, you will get the following error:
rmdir: failed to remove 'dir1': Directory not emptyIn 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:
rmdir dir1 dir2 dir3To remove a directory and its empty parent directories, use the -p option:
rmdir -p parent/child/grandchildThis 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:
rmdir -p --ignore-fail-on-non-empty parent/child/grandchildRemoving 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:
rm -r dir1If 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:
rm -rf dir1rm -rf deletes without asking and without a way back. Never run it against /, and be careful with paths built from variables. When $DIR is empty or unset, rm -rf "$DIR"/* becomes rm -rf /*. GNU rm refuses to act on / by default, but that guard does not cover /*. Writing the path as "${DIR:?}"/* makes the shell stop with an error when the variable is empty.To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space:
rm -r dir1 dir2 dir3On a large tree it helps to see what actually went away. The -v (verbose) option prints each entry as it is removed:
rm -rv dir1removed directory 'dir1/logs'
removed 'dir1/notes.txt'
removed directory 'dir1'The output shows the empty logs directory and notes.txt being removed first, followed by 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:
rm -rI dir1To remove the directory, type y and hit Enter:
rm: remove 1 argument recursively? yTo remove directories whose names end with _bak from just the current directory, first preview the matches with find.
find . -maxdepth 1 -type d -name '*_bak' -printThe -maxdepth 1 option keeps the search at the current level, and -type d limits the matches to directories. Check the list before passing the same matches to rm -r.
find . -maxdepth 1 -type d -name '*_bak' -exec rm -r -- {} +With GNU rm, --one-file-system skips nested directories on a different filesystem from the starting path. rm can still delete files through a bind mount on the same filesystem or inside a mount point you pass directly as the starting path. To skip a separate filesystem mounted at /mnt/staging/backup, start at /mnt/staging.
rm -rf --one-file-system /mnt/stagingIn this example, rm reports that it skipped /mnt/staging/backup.
rm: skipping '/mnt/staging/backup', since it's on a different devicerm exits with status 1, and /mnt/staging itself stays in place. It still contains the skipped mount point.
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:
find . -type d -name '*_cache' -pruneOnce 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:
find . -type d -name '*_cache' -prune -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.-prune- Stopsfindfrom descending into a matched directory. Without it, a nested match such asa_cache/b_cachereachesrmafter its parent is already gone, andrmreports “No such file or directory”.-exec rm -r {} +- Executesrm -ron all matched directories.
Removing All Empty Directories
Preview the empty directories with the same depth-first traversal that the deletion command will use:
find /dir -depth -mindepth 1 -type d -empty -printThis 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:
find /dir -depth -mindepth 1 -type d -empty -deleteHere is an explanation of the options used:
/dir- Recursively search in the/dirdirectory.-depth- Process each directory’s contents before the directory itself.-mindepth 1- Keep the starting directory/dirout of the matches.-type d- Restricts the search to directories.-empty- Restricts the search only to empty directories.-delete- Removes entries that match the preceding tests. A directory must be empty for removal to succeed.
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:
rm -r "my old backups"Escaping each space with a backslash does the same job:
rm -r my\ old\ backupsNames 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:
rm -r -- -dirPrefixing the path with ./ avoids the ambiguity just as well, and it is easier to remember in the middle of a cleanup:
rm -r ./-dirRemoving a Symbolic Link That Points to a Directory
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:
rm mylinkThe 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:
rm: cannot remove 'mylink/': Is a directoryReaching 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:
rm -r -- /dirThis 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. If rm -r reports this error at a mount point, it may already have deleted files inside the mounted filesystem. Use --one-file-system to skip nested mounts on a different filesystem from the starting path. 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. To move a directory to the Trash from the command line, use gio trash <path> or trash-put.
For more details, see our guides on the rm command and removing files and directories .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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