Skip to content

How to Use rm Command

homepage-banner

This guide shows how to use rm to remove files, directories, and other content from the command line in Linux.

Note: To avoid creating examples that might remove important files, this Quick Answer uses variations of filename.txt. Adjust each command as needed.

The Basics of Using rm to Delete a File

  • Delete a single file using rm:

    rm filename.txt
    
  • Delete multiple files:

    rm filename1.txt filename2.txt
    
  • Delete all .txt files in the directory:

    rm *.txt
    

Options Available for rm

i Interative mode

Confirm each file before delete:

rm -i filename.txt

f Force

Remove without prompting:

rm -f filename.txt

v Verbose

Show report of each file removed:

rm -v filename*.txt

d Directory

Remove the directory:

rm -d filenames/

Note: This option only works if the directory is empty. To remove non-empty directories and the files within them, use the r flag.

r Recursive

Remove a directory and any contents within it:

rm -r filenames/

Combine Options

Options can be combined. For example, to remove all .png files with a prompt before each deletion and a report following each:

rm -iv *.png
remove filename01.png? y
filename01.png
remove filename02.png? y
filename02.png
remove filename03.png? y
filename03.png
remove filename04.png? y
filename04.png
remove filename05.png? y
filename05.png

rf Remove Files and Directories, Even if Not Empty

Add the f flag to a recursive rm command to skip all confirmation prompts:

rm -rf filenames/

Combine rm with Other Commands

Removing Old Files Using find and rm

To find and remove all files older than 28 days, combine the find command’s -exec option with rm. The files that match are printed on the screen using -print:

find filename* -type f -mtime +28 -exec rm '{}' ';' -print

In this command, {} is replaced by the find command with all files that it finds, and ; tells find that the command sequence invoked with the -exec option has ended. Specifically, -print is an option for find, not the executed rm. Surround {} and ; with single quote marks to protect them from interpretation by the shell.

Leave a message