How to Use rm and rmdir to Delete Files and Directories in Linux
Learn how to use rm and rmdir to delete files and directories in Linux. Covers rm flags (-r, -f, -i), rmdir for empty directories, the danger of rm -rf, and safe deletion practices. Based on GNU coreutils documentation.
- Difficulty
- Beginner
- Time required
- 10 minutes
- Steps
- 5
- Platform
- Linux (GNU coreutils)
- Last tested
- July 19, 2026

Table of contents
Quick Answer
Go to the stepsUse rm file.txt to delete a file, rmdir emptydir to remove an empty directory, and rm -r directory to delete a directory and everything in it. Add -i for confirmation prompts and -f to force deletion. Treat rm -rf with extreme caution, as it deletes everything under a path with no undo.
- Delete a file: rm file.txt
- Delete an empty directory: rmdir emptydir
- Delete a directory and its contents: rm -r directory
- Confirm each deletion: rm -i file.txt
- Force delete without prompts: rm -f file.txt (use with caution)
Linux terminalrm file.txtExpected result: The specified file or directory is permanently deleted.
Key takeaways
- You'll learn how to delete files and directories in Linux with rm and rmdir, including recursive and force options, and how to avoid catastrophic mistakes with rm -rf.
- rm and rmdir permanently delete data with no recycle bin. Understanding their flags prevents accidental data loss, especially the destructive rm -rf which can wipe entire directory trees.
- Use rmdir for empty directories and rm for files. Add -r to rm for directories with contents, -i for confirmation prompts, and treat rm -rf with extreme caution.
Introduction
Deleting files and directories is a routine Linux task, but the command line offers no recycle bin. The rm and rmdir commands remove files and directories permanently, so understanding their flags and safety implications matters.
The rm command deletes files and, with the recursive flag, entire directory trees. The rmdir command is more limited: it only removes empty directories, which makes it a safer choice when you expect a directory to be empty and want the command to fail otherwise.
This tutorial covers both commands, their common flags (-r, -f, -i), the notorious danger of rm -rf, and safe deletion practices. The behavior described follows the GNU coreutils implementation found on most Linux distributions.
Who this is for: Linux beginners and administrators who want to understand file deletion commands and their safety implications.
Before you start
- Access
- A Linux shell (terminal). Write permission on the target files and their parent directories.
- Required roles
- Standard user (root for system files)
- Standard user
- Vendor
- GNU/Linux
- Last tested
- Backup required
10 minutes
Critical: rm deletes permanently, no undo
rm permanently deletes files. There is no recycle bin on the Linux command line. Deleted files are not easily recoverable. Double-check paths before running rm, especially with -r or -f.
Critical: rm -rf is extremely dangerous
rm -rf on the wrong path can wipe entire directory trees, including system files if run as root. Never run rm -rf on a path you haven't verified. Consider a backup before bulk deletions.
1Delete a File with rm
Delete a single file with rm.
To delete a file, run rm followed by the file name:
rm file.txtYou can delete multiple files at once:
rm file1.txt file2.txt file3.txtrm produces no output on success. Use ls to confirm the files are gone.
rm file.txtExpected result: The file is deleted. No output on success.
Note
rm deletes permanently. There is no recycle bin. Verify the file name before running.
2Remove an Empty Directory with rmdir
Remove an empty directory with rmdir.
To remove an empty directory, use rmdir:
rmdir emptydirPer the GNU coreutils documentation, rmdir only removes empty directories. If the directory contains files or subdirectories, it fails with 'Directory not empty.' This makes rmdir a safe choice when you expect a directory to be empty.
rmdir emptydirExpected result: The empty directory is removed. Fails if the directory contains anything.
Note
Use rmdir -p to remove a directory and its empty parent directories in one command (e.g., rmdir -p a/b/c removes c, then b, then a if each is empty).
3Delete a Directory Recursively
Delete a directory and its contents recursively.
To delete a directory that contains files, use rm with the recursive flag -r (or -R):
rm -r directoryThis deletes the directory and everything inside it, including subdirectories. Unlike rmdir, rm -r works on non-empty directories.
rm -r directoryExpected result: The directory and all its contents are deleted.
Note
Double-check the directory path. rm -r on the wrong directory deletes everything under it. There is no undo.
4Delete with Confirmation (-i)
Delete files with a confirmation prompt.
To delete files safely with a confirmation prompt, use the interactive flag -i:
rm -i file.txtrm asks 'remove file.txt?' before deleting. Type y to confirm or n to skip. This is useful when deleting multiple files or when you want a safety check.
rm -i file.txtExpected result: rm prompts for confirmation before deleting each file.
Note
You can combine flags: rm -ri directory prompts before each file in a recursive delete. Some users alias rm to 'rm -i' for safety.
5The Force Flag and rm -rf Danger
Understand the force flag and the danger of rm -rf.
The force flag -f deletes files without prompting, even write-protected ones, and ignores non-existent files:
rm -f stubborn-file.txtWarning: Combining -r and -f creates rm -rf, which recursively and forcibly deletes a path with no confirmation. Per the GNU coreutils documentation, this is powerful and dangerous. A typo like rm -rf / home/user (note the space) can be catastrophic. Always verify the exact path before running rm -rf.
rm -f stubborn-file.txtExpected result: The file is deleted without prompts, even if write-protected.
Note
Never run rm -rf on a path you haven't verified. Consider printing the target first with ls or echo, and keep backups before bulk deletions.
Verify the Deletion
Verify fix
After running rm or rmdir, the target file or directory is permanently removed. There is no confirmation on success (unless you use -v for verbose). To confirm, list the directory with ls and verify the item is gone.
If you see 'Permission denied,' you lack write permission on the file or its parent directory. Use sudo (carefully) or adjust permissions. If rmdir reports 'Directory not empty,' the directory still contains files; use rm -r to delete it and its contents, or remove the contents first.
Remember that deletion is permanent. Per the GNU coreutils documentation, rm does not move files to a recycle bin. If you deleted something important, stop writing to that filesystem and consider recovery tools, though success isn't guaranteed.
Normal result: The file or directory is removed. ls confirms it no longer exists.
Abnormal result: 'Permission denied' (need write access or sudo) or 'Directory not empty' (use rm -r or empty it first).
Successful file deletion
rm file.txt (no output, file gone)
File deleted successfully.
Directory not empty
rmdir: failed to remove 'dir': Directory not empty
rmdir on a non-empty directory.
Troubleshooting
Permission denied when running rm
Warning
Cause: You lack write permission on the file or its parent directory.
Check permissions with ls -l. Use sudo rm (carefully) if you need elevated privileges, or change ownership/permissions. Only use sudo when you're certain of the path.
rmdir: Directory not empty
Note
Cause: The directory contains files or subdirectories, and rmdir only removes empty directories.
Use rm -r to delete the directory and its contents, or remove the contents first with rm, then use rmdir.
Accidentally deleted an important file
Critical
Cause: rm doesn't move files to a recycle bin. Deletion is permanent.
Stop writing to that filesystem immediately. Recovery tools like testdisk or extundelete may help but success isn't guaranteed. Restore from backup if available. Prevent recurrence with rm -i and regular backups.
Frequently asked questions
What is the difference between rm and rmdir?
rm deletes files (and directories with -r). rmdir only removes empty directories and fails if the directory contains anything, making it a safer way to remove directories you expect to be empty.
Can I recover files deleted with rm?
No. The Linux command line has no recycle bin. rm permanently deletes files with no built-in undo. Recovery requires specialized tools and isn't guaranteed. Keep backups.
Why is rm -rf dangerous?
rm -rf recursively (-r) and forcibly (-f) deletes a path and everything under it, with no confirmation prompts. It's dangerous because a typo or wrong path can wipe entire directory trees, including system files if run as root.
How can I delete files more safely?
Use rm -i, which prompts for confirmation before each deletion. You can also alias rm to rm -i in your shell profile, though relying on aliases can create false confidence.
Why does rmdir fail on my directory?
rmdir fails with 'Directory not empty'. Use rm -r to delete a non-empty directory and its contents, or empty the directory first.
Conclusion
The rm command deletes files and, with -r, directories and their contents. The rmdir command only removes empty directories, making it a safer choice when you expect a directory to be empty.
Use -i for interactive confirmation, -f to force deletion without prompts, and -r (or -R) for recursive directory removal. Per the GNU coreutils documentation, rm -rf is powerful and dangerous: it silently deletes everything under a path with no confirmation. Always verify paths and keep backups.
Use rmdir for empty directories and rm for files. Add -r to rm for directories with contents, -i for confirmation prompts, and treat rm -rf with extreme caution.
rm file.txt / rmdir emptydir / rm -r directory



