rm Command in Linux: Remove Files and Directories

By 

Updated on

7 min read

Using the rm command to remove files and directories in Linux

When you clean up old logs, build artifacts, or temporary files from a terminal, rm is usually the command you reach for. It removes files and directories from the filesystem, and it does so permanently rather than sending them to a desktop Trash folder.

This guide explains how to use the rm command in Linux through practical examples, including recursive directory removal, confirmation prompts, glob patterns, and safer ways to handle rm -rf.

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.

Common rm Options

The most common rm options control recursion, prompting, and output:

  • -f, --force - Ignore nonexistent files and never prompt.
  • -i - Prompt before every removal.
  • -I - Prompt once before removing more than three files, or before recursive removal.
  • -r, -R, --recursive - Remove directories and their contents recursively.
  • -d, --dir - Remove empty directories.
  • -v, --verbose - Print each removal as it happens.
  • --interactive=WHEN - Set prompting behavior to never, once, or always.
  • --one-file-system - During recursive removal, skip directories on a different filesystem.
  • --preserve-root - Refuse to remove /, which is the default protection on GNU rm.

You can combine short options. For example, rm -rv dirname removes a directory recursively and prints each removed path.

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'

The verbose output is useful when a command matches several files and you want a record of what was removed.

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

The shell expands *.png before rm runs. If the pattern matches more files than expected, adjust the pattern before you delete anything.

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, including 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

The order of the short options does not matter, so rm -fr dirname does the same thing as rm -rf dirname.

Warning
rm -rf is permanent and irreversible. Double-check the path before running it. Never run rm -rf / or rm -rf /*, because 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.

When using variables in scripts, print the target path before removing it or test it with ls first. This helps catch empty or unexpected variables before they are passed to rm -rf.

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
Recursive verbose removalrm -rv dir
Prompt before each filerm -i file
Single prompt for many filesrm -I file1 file2 ...
Remove file starting with -rm -- -filename
Prompt behavior by namerm --interactive=once file1 file2

Troubleshooting

Operation not permitted
You may not have write permission on the parent directory, or the directory may use sticky-bit rules such as /tmp. Check the parent directory with ls -ld, verify ownership, and use sudo only when you need root privileges.

Permission denied
The parent directory permissions usually control whether you can remove an entry. Check the directory with ls -ld dirname, then adjust permissions with chmod or ownership with chown when appropriate.

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.

Immutable file cannot be removed
On Linux filesystems that support file attributes, an immutable file cannot be removed even by root until the attribute is cleared. Check it with lsattr filename, and remove the immutable flag with sudo chattr -i filename only if you are sure the file should be deleted.

Device or resource busy
The path may be a mount point, or a process may be using the directory. Check mounted filesystems with findmnt and active users of the path with lsof +D dirname before trying again.

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 rm -rf dirname/* to remove non-hidden contents while leaving the directory itself in place. Shell globs such as * do not match hidden dotfiles by default. To remove hidden and non-hidden contents without deleting the parent directory, use find dirname -mindepth 1 -delete after verifying the path.

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

Before any rm -rf, run the same path through ls first, especially when variables are involved. For frequent cleanup of build artifacts or logs, alias rm to trash from trash-cli so accidental deletions stay recoverable.

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