The Basic Command: rmdir and rm
To delete an empty directory in Linux, use the rmdir command followed by the directory name. For example, rmdir my_folder removes the directory called my_folder, but only if it contains nothing. If the directory has files or subdirectories inside it, rmdir will refuse and show an error message.
To delete a directory that contains files, use rm -r (the -r flag means "recursive"). The command rm -r my_folder deletes my_folder and everything inside it — all files and subdirectories. This is permanent; Linux does not move deleted items to a trash bin where you can recover them later.
A safer variation is rm -ri my_folder, which asks you to confirm each file before deleting it. This takes longer but gives you a chance to stop if you realize you are removing the wrong thing.
Key Takeaways
- Use rmdir only for empty directories; it will fail if anything is inside.
- Use rm -r to delete a directory and all its contents in one command.
- Deleted files do not go to trash — they are gone immediately, so double-check the path before you press Enter.
- Add the -i flag (rm -ri) to be asked before each deletion, which slows the process but prevents accidents.
- You must have write permission on the directory itself to delete it; check with ls -ld if you get a permission error.
When rmdir Works and When It Does Not
The rmdir command is the gentler option because it refuses to delete anything with contents. If you type rmdir my_folder and the folder has even one file inside, you will see an error like "Directory not empty" and nothing will be deleted. This makes rmdir useful as a safety check — if you meant to empty the folder first, rmdir stops you before you lose data.
However, rmdir only works on directories you own or have write permission for. If you try to delete a directory owned by another user, you will get a "Permission denied" error even if the directory is empty. The owner or root can delete it, but you cannot.
Using rm -r for Directories With Contents
The rm -r command deletes the directory and everything inside it without asking. This includes subdirectories nested several levels deep. One command like rm -r project_folder will remove project_folder, all folders inside it, and all files at every level.
Because this is irreversible, take a moment to verify the path. A common mistake is typing rm -r /home/user/documents when you meant rm -r /home/user/documents/old_project. The first command deletes your entire documents folder. The second deletes only the old_project subfolder inside it. Check the path twice before pressing Enter.
If you want to see what will be deleted before it happens, run ls -R directory_name first. The -R flag shows the directory and everything inside it, so you can verify you are removing the right thing.
The -i Flag: Asking Before Each Deletion
Adding -i to the rm command makes it ask for confirmation before deleting each item. Type rm -ri my_folder and Linux will prompt you for each file and subdirectory, showing something like "remove regular file 'document.txt'?" You type y to delete it or n to keep it.
This is slower — if the folder has hundreds of files, you will be answering prompts for a while — but it gives you control. You can stop partway through if you realize the folder contains something you need to keep. For large directories, this is not practical, but for smaller ones it is a good safety net.
Checking Permissions Before Deletion
If you try to delete a directory and get "Permission denied", you do not have write permission on that directory. Use ls -ld directory_name to see who owns it and what permissions are set. The output looks like drwxr-xr-x user group. If the owner is not you, you cannot delete it unless you are root or the owner gives you permission.
If you own the directory but still cannot delete it, check the permissions on the parent directory — the folder that contains it. You need write permission on the parent folder to delete anything inside it. For example, to delete a folder in /home/user/documents, you need write permission on /home/user/documents itself, not just on the folder you are removing.
Deleting Multiple Directories at Once
You can delete several directories in one command by listing them. For example, rm -r folder1 folder2 folder3 deletes all three. This works whether the folders are empty or full. You can also use a wildcard: rm -r old_* deletes every directory whose name starts with "old_".
Wildcards are powerful but risky. Before running a command with a wildcard, test it with ls old_* first to see exactly which directories match the pattern. If the list looks wrong, do not run the rm command. This extra step takes five seconds and prevents mistakes that cannot be undone.
What Happens to Disk Space After Deletion
When you delete a directory with rm -r, the space it occupied on disk becomes available for new files. Linux does not keep a record of what was deleted or hold the space in reserve. If you realize minutes later that you deleted the wrong folder, the space may already be overwritten by new data, making recovery impossible.
If you think you might need to restore a deleted directory, your only option is a backup. Some systems keep automated backups on a separate drive or server. Check with your system administrator or look in /backup or /snapshots to see if your Linux system keeps old versions of files. If no backup exists and the deletion was recent, a data recovery service might be able to retrieve some files, but this is expensive and not always successful.
Frequently Asked Questions
Can I undo a deletion after I run rm -r?
No. Linux does not move deleted files to a trash bin. Once rm -r completes, the directory and its contents are gone. Your only recovery option is a backup made before the deletion. Check if your system administrator maintains backups before assuming the data is lost.
What is the difference between rm -r and rm -rf?
The -f flag means "force" and tells rm to delete without asking, even if files are read-only. Use rm -rf only when you are certain, because it bypasses all safety prompts. For most tasks, rm -r or rm -ri is safer.
Why does rmdir say "Directory not empty" when I think it is empty?
Hidden files (those starting with a dot, like .config) are not shown by default in file listings. Use ls -a directory_name to see all files, including hidden ones. If you see hidden files, delete them first or use rm -r instead of rmdir.
Do I need to be root to delete a directory?
Only if you do not own the directory or lack write permission on its parent folder. If you own the directory, you can delete it. If you do not own it, ask the owner to delete it or ask a system administrator to do so.
What if a directory name has spaces or special characters?
Put the name in quotes: rm -r "my folder" or rmdir "my folder". Without quotes, Linux treats each word as a separate directory name and tries to delete multiple things, which usually fails.