rm Command in Linux: Remove Files and Directories

By 

Updated on

5 min read

Using the rm command to remove files and directories in Linux

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:

txt
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:

Terminal
rm filename

If 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:

output
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:

Terminal
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:

Terminal
rm -v filename
output
removed 'filename'

Remove Multiple Files

Unlike the unlink command, rm accepts multiple filenames at once:

Terminal
rm filename1 filename2 filename3

You can also use shell glob patterns to match multiple files. For example, to remove all .png files in the current directory:

Terminal
rm *.png

Before running rm with a glob pattern, use ls to preview which files will be matched:

Terminal
ls *.png

Remove Directories

To remove one or more empty directories, use the -d option:

Terminal
rm -d dirname

rm -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:

Terminal
rm -r dirname

This 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:

Terminal
rm -i filename1 filename2
output
rm: 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:

Terminal
rm -I filename1 filename2 filename3 filename4
output
rm: 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:

Terminal
rm -rf dirname
Warning
rm -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:

Terminal
rm -- -filename

Alternatively, prefix the path with ./:

Terminal
rm ./-filename

Quick Reference

For a printable quick reference, see the rm cheatsheet .

TaskCommand
Remove a filerm file
Remove without promptingrm -f file
Verbose outputrm -v file
Remove multiple filesrm file1 file2 file3
Remove by glob patternrm *.log
Remove empty directoryrm -d dir
Remove directory recursivelyrm -r dir
Remove recursively, no promptrm -rf dir
Prompt before each filerm -i file
Single prompt for many filesrm -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 directory
rm 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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