Use rm to Delete Files from the Linux Command Line

Updated by Edward Angert Written by Edward Angert

Contribute on GitHub

Report an Issue | View File | Edit File

Use rm to remove files and directories from the command line.

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 Interactive 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
remove filename02.png? y
remove filename03.png? y
remove filename04.png? y
remove filename05.png? y

-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

Remove Old Files Using find and rm

Combine the find command’s -exec option with rm to find and remove all files older than 28 days old. The files that match are printed on the screen (-print):

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

In this command’s syntax, {} 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. In particular, -print is an option for find, not the executed rm. {} and ; are both surrounded with single quote marks to protect them from interpretation by the shell.

Join our Community

Find answers, ask questions, and help others.

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.