In Linux, how do I remove a directory? This is a typical query from Linux novices. It's certain that as you get used to using the Linux operating system, you'll wish to delete a directory. If you use the GUI, removing a directory in Linux is a rather straightforward process. However, you can also delete directories via terminal commands if you don't have access to the GUI.
Quick answer: use rmdir if the directory is empty, rm -r if it still holds files, and find when you need to clear many directories by name or age. All three skip the Trash. Once a command finishes, the data is gone unless you have a backup.
Before you start, you need a machine that runs a Linux distribution, a user account with sudo rights, and access to the command line or terminal window. If you're new to the terminal, our cheat sheet of basic Linux commands covers everything from ls and cd to rm. If you want to learn more about removing directories in Linux, you can check out our detailed guide on how to remove a directory in Linux.
โก How to Delete a Directory in Linux
Quick answer: commands to delete a directory in Linux
| Situation | Command |
| Empty directory | rmdir dir |
| Non-empty directory | rm -r dir |
| Non-empty, no prompts | rm -rf dir |
| Ask before each file | rm -ri dir |
| All empty directories in a tree | find /path -depth -mindepth 1 -type d -empty -delete |
Run pwd and ls before any of these. A typo in the path is the most common way people lose data.
When to use rm vs rmdir vs find
Pick the least powerful command that does the job. rmdir refuses to touch anything with contents, so it's the safe default. rm -r removes everything under the path you give it. find picks targets by pattern, and a loose expression can match far more than you meant to delete.
| Command | Best for | Risk level |
rmdir |
Empty folders, tidying leftover structure | Low |
rm -r |
A folder and everything in it | Medium to high |
find ... -delete or -exec rm -r |
Bulk cleanup by name, age, or emptiness | High if you skip the preview |
How to use the rm command to delete a directory in Linux?
In Linux, the rm command is used to remove files and directories. This command can be used to completely delete a directory, which deletes the directory together with all of its files and subdirectories. The syntax is:
rm [options] [files and/or directories]
Add several file or directory names, separated by blank spaces, to the rm command to remove numerous files or directories. Don't forget that an error message appears when attempting to remove a directory using the rm command without any parameters.
The rm command can be modified by adding the -d parameter to remove an empty directory:
rm -d Example
To remove a directory with files and subdirectories, use the -r flag. When the -v flag is used, the output describes each stage of the operation, whereas the -r flag deletes the entire directory, including subdirectories and files:
rm -r -v Example
A prompt requesting you to confirm directory removal is displayed when you use the -i option. To confirm, enter Y and then hit Enter.
rm -d -i Example
When removed, write-protected directories demand user input. Make such a directory by using:
sudo mkdir Example
To delete a directory in Linux, use the following:
rm -d Example
To confirm the deletion, enter Y and then hit Enter. Use the -f switch or increase the command privileges to sudo to avoid the confirmation:
rm -d -f Example
sudo rm -d Example
Use the following command if there are other files and directories in the write-protected directory:
rm -rf <directory name>
rm -rf / makes your system unusable by forcing a recursive deletion on the root directory. GNU's rm refuses that exact command by default because of --preserve-root, but rm -rf /* still wipes the system, because the shell expands the glob before rm sees it. When removing folders, avoid using sudo and -f unless you are sure what you are doing.
How to delete a non-empty directory in Linux
rm -r myfolder
rm -ri myfolder
rm -rf myfolder
Here's what each flag does, per the GNU Coreutils rm manual:
-rremoves the directory and everything inside it, recursively.-iasks before every removal. It's tedious on big trees but good for a folder you only half recognize.-Iasks once before removing more than three files or before deleting recursively. I'd use this as your everyday safety net, since it catches mistakes without nagging.-fsuppresses prompts and ignores files that don't exist.
Save rm -rf for paths whose contents you already know, such as a build directory or a cache folder your script created. Be extra careful with sudo rm -rf. With root privileges nothing stops you, and a stray space (sudo rm -rf / var/tmp/old) changes the target completely. If you also need to remove packages as part of cleanup, our guide on using the apt remove command covers that side.
Removing a directory in Linux using rmdir command
Empty directories can be removed with the rmdir command:
rmdir [options] [directory name]
The command equals rm with the -d flag: rm -d. You receive the following error when using rmdir on a non-empty directory:
RMDIR: [folder]: Not an empty directory.
Use this command without any parameters to remove an empty directory:
rmdir test
The -p flag of the rmdir command enables you to remove a directory with its parent in the tree. For instance, imagine your file system looks like this:
> Test
---> Test2
The Test2 subfolder is located in the directory Test. Test becomes an empty directory if the Test2 directory is removed. Thus as opposed to:
rmdir Test/Test2 Test
Use the -p flag as follows:
rmdir -p Test/Test2
With this command, Test2 and its parent, Test, will be deleted from the tree. But if either directory is not empty, this command will fail.
How to delete an empty directory in Linux
rmdir myfolder
If myfolder contains even one hidden file, rmdir stops with "Directory not empty." That refusal means you can't wipe data you forgot was there. To remove a chain of empty directories in one go, add -p:
rmdir -p projects/2023/drafts
This removes drafts, then 2023, then projects, and stops at the first one that still has contents.
Removing a directory in Linux with find
With the help of the command-line tool named find, you may look for directories based on an expression and take action on each matching directory. The most typical scenario is to delete directories based on a pattern. Run the following command, for instance, to remove all folders in the current working directory that finish in the character _cache:
find . -type d -name '*_cache' -exec rm -r {} +
You can run the following to clear every empty directory in a directory tree:
find /dir -type d -empty -delete
Be careful when you decide to use the -delete option. If the -delete option is added first, the search command will erase everything below the beginning points you specified. The find command line is processed as an expression. Use -delete as the last option and always test the command without it.
If you accidentally delete important files, don't worry โ there are ways to recover them. Check out our guide on how to recover deleted files in Linux to ensure you can retrieve lost data safely.
How to delete directories with find
Run the expression without a delete action first, so you can see what matches:
find . -type d -name '*_cache'
Once the list looks right, add the deletion:
find . -type d -name '*_cache' -exec rm -r {} +
To clear out empty directories, or ones untouched for 30 days:
find /dir -depth -mindepth 1 -type d -empty -delete
find /backups -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -r {} +
The GNU Findutils manual spells out the rules for -delete. It implies -depth, so find processes contents before their parent, and it only removes directories that are already empty. Because find evaluates its expression left to right, putting -delete first deletes everything under the starting path before your filters run, so always put it last. -mindepth 1 protects the starting directory itself.
If you automate this with cron, schedule it only after you've run the preview version by hand and checked its output.

๐ก๏ธ Safe ways to delete directories in Linux
- Run
pwdto confirm where you are before using relative paths. - Run
ls -la dirto see the contents, including hidden files. - Run
du -sh dirto check the size. If you expected 20 MB and see 40 GB, stop. - Preview
findexpressions and wildcards withlsorechofirst. - Only use
sudowhen a permission error proves you need it.
GNU rm has two built-in guards. --preserve-root is on by default and makes rm -r / fail. It doesn't protect against rm -rf /*, because the shell expands the glob into a list of individual directories before rm runs. The second guard, --one-file-system, skips directories on a different mounted filesystem, which helps when the tree you're deleting has a bind mount or network share inside it, like a chroot.
Delete directories with spaces, special characters, or leading dashes
rm -r "My Folder"
rm -r My\ Folder
rm -r -- -example
Without quotes, rm -r My Folder tries to delete two separate things called My and Folder. A name starting with a dash looks like an option, so -- tells rm that the options are finished. rm -r ./-example works too. Wildcards need the same care. rm -r temp* also matches template and temp_important, so run ls -d temp* first and read the list.
Common errors and how to fix them
| Error | Cause | Fix |
| Directory not empty | rmdir found files, often hidden ones |
Check with ls -la, then use rm -r |
| Permission denied | No write and execute permission on the parent directory, or a sticky bit (as on /tmp) blocking you from deleting another user's entries |
Check with ls -ld on the parent, fix ownership with chown, or use sudo if the files really belong to the system |
| No such file or directory | Wrong path, typo, or an unquoted space | Use tab completion and quote the name |
| Device or resource busy | The directory is a mount point, or a process has it open | Run umount, or find the process with lsof +D dir. If you need a deeper walkthrough, see our guide on finding which process is using a file in Linux. |
The parent-directory rule surprises people. Deleting an entry changes its parent directory, so the parent's permissions are what count, as the rmdir(2) man page explains. If the directory belongs to a user you no longer need, removing the account entirely may be the right call โ our guide on how to remove users in Linux covers userdel and the cleanup steps that go with it.
๐ Can you recover a deleted directory in Linux?
Usually not easily. rm unlinks the files, and the filesystem is free to reuse that space immediately. If you delete something by mistake, stop writing to that disk and unmount it if you can. Then restore from a backup or snapshot (LVM, Btrfs, ZFS, or your VPS provider's snapshot feature). Recovery tools sometimes get files back from ext4, but don't count on them. A tested backup is the only reliable undo. On desktop systems you can send folders to the Trash from the terminal with gio trash dir on GNOME-based distros, or with the trash-cli package.
๐ฏ Conclusion
After reading this guide, you should be able to use the terminal window or command line to delete a directory in Linux. Linux allows you to remove directories using the commands rm and rmdir. Non-empty folders are deleted by rm. Empty directories are removed by the rmdir command. However, this command cannot delete a directory with files inside of it.
When you want to delete a folder, you might be used to dragging it to the trash on your computer. However, you should be aware that there is no trash can on the Linux command line. After removing a directory in Linux, it's permanently erased. This is particularly important when working with a Linux VPS, where you might be managing critical directories and accidental deletions could lead to significant data loss. Always double-check your commands to ensure you are deleting the correct directories.
If you're also working with files and need to delete them individually, it's important to understand the proper commands for removing files as well. For detailed steps on how to safely delete a file in Linux, you can check out our guide on how to delete a file in Linux.


Leave A Comment