rm Command in Linux: Remove Files and Directories

The rm command removes files and directories from the filesystem. It is one of the most essential Linux commands — and one of the most dangerous, since deleted files cannot be easily recovered.
This guide explains how to use rm through practical examples covering the most common options.
rm Command Syntax
The general syntax for the rm command is:
rm [OPTIONS] FILE...By default, rm does not remove directories and does not prompt for confirmation before deleting files.
Remove a Single File
To delete a single file, pass the filename as an argument:
rm filenameIf the file is not write-protected, it is removed without any output. On success, rm returns exit code zero.
If you do not have the required permissions on the parent directory (and in some cases due to sticky-bit rules), you will get an “Operation not permitted” error.
When removing a write-protected file, rm prompts for confirmation:
rm: remove write-protected regular empty file 'filename'?Type y and press Enter to confirm.
To skip the prompt and remove files without confirmation, use the -f (force) option:
rm -f filename-f also silently ignores nonexistent files instead of returning an error.
To see each file as it is removed, use the -v (verbose) option:
rm -v filenameremoved 'filename'Remove Multiple Files
Unlike the unlink
command, rm accepts multiple filenames at once:
rm filename1 filename2 filename3You can also use shell glob patterns to match multiple files. For example, to remove all .png files in the current directory:
rm *.pngBefore running rm with a glob pattern, use ls
to preview which files will be matched:
ls *.pngRemove Directories
To remove one or more empty directories, use the -d option:
rm -d dirnamerm -d is functionally identical to the rmdir
command.
To remove a non-empty directory and all of its contents recursively, use the -r (recursive) option:
rm -r dirnameThis deletes the directory and everything inside it — all files, subdirectories, and symbolic links.
Prompt Before Removal
The -i option prompts for confirmation before removing each file:
rm -i filename1 filename2rm: remove regular empty file 'filename1'?
rm: remove regular empty file 'filename2'?Type y and press Enter to confirm each file.
When removing more than three files or recursively removing a directory, use the -I option (uppercase) to get a single prompt for the entire operation instead of one per file:
rm -I filename1 filename2 filename3 filename4rm: remove 4 arguments?rm -rf
Combining -r and -f removes a directory and all its contents without any prompts, even if files inside are write-protected:
rm -rf dirnamerm -rf is permanent and irreversible. Double-check the path before running it. Never run rm -rf / or rm -rf /* — this will attempt to delete the entire filesystem. Modern Linux systems protect against rm -rf / by default, but the protection does not cover all variations.Remove Files with Names Starting with a Dash
Filenames that begin with - are interpreted as options by rm. To remove such a file, use -- to signal the end of options:
rm -- -filenameAlternatively, prefix the path with ./:
rm ./-filenameQuick Reference
For a printable quick reference, see the rm cheatsheet .
| Task | Command |
|---|---|
| Remove a file | rm file |
| Remove without prompting | rm -f file |
| Verbose output | rm -v file |
| Remove multiple files | rm file1 file2 file3 |
| Remove by glob pattern | rm *.log |
| Remove empty directory | rm -d dir |
| Remove directory recursively | rm -r dir |
| Remove recursively, no prompt | rm -rf dir |
| Prompt before each file | rm -i file |
| Single prompt for many files | rm -I file1 file2 ... |
Remove file starting with - | rm -- -filename |
Troubleshooting
Operation not permitted
You do not have write permission on the parent directory. Check permissions with ls -ld on the parent directory, and use sudo if needed.
cannot remove 'dirname': Is a directoryrm requires the -r flag to remove directories. Run rm -r dirname instead.
cannot remove 'file': No such file or directory
The file does not exist or the path is incorrect. Use ls to verify the path. To suppress this error in scripts, use rm -f.
Write-protected file is not removed
Without -f, rm prompts before removing write-protected files. Either type y to confirm, or use rm -f to skip the prompt.
FAQ
Can I recover a file deleted with rm?
Not easily. rm removes the filesystem entry immediately without moving the file to a trash folder. Recovery may be possible with tools like extundelete or testdisk on ext4, but success depends on whether the disk blocks have been overwritten. For safer deletion, use a trash utility such as trash-cli (trash file) which moves files to ~/.local/share/Trash.
What is the difference between rm and unlink?unlink
removes a single file by calling the unlink system call directly. rm is a higher-level utility that supports multiple files, directories, glob patterns, and interactive prompting.
How do I remove all files in a directory without removing the directory itself?
Use a glob: rm -rf dirname/*. This removes all contents but leaves the empty directory in place.
Is rm -rf / dangerous?
Yes. It attempts to delete everything on the filesystem. Modern Linux systems block it by default with a --preserve-root safeguard, but variations like rm -rf /* or specifying a mounted root path are not always protected. Never run this command.
How do I delete files older than N days?
Use the find
command: find /path -type f -mtime +30 -delete removes files not modified in the last 30 days.
Conclusion
The rm command removes files and directories permanently. Use -r for recursive removal, -f to skip prompts, and -i or -I for interactive confirmation. Always verify the path before running rm -rf, as the operation cannot be undone.
If you have any questions, feel free to leave a comment below.
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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page