How to delete a file using rm command in Linux / Unix

See all GNU/Linux related FAQ
How do I delete a file under a Linux / UNIX / *BSD / AIX / HP-UX operating system using command line options?

To remove or delete a file or directory in Linux, FreeBSD, Solaris, macOS, or Unix-like operating systems, use the rm command or unlink command. This page explains how to delete a given file on a Linux or Unix like system using the command line option.
How to delete Files Using Linux Or Unix Command Line
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category File Management
Prerequisites rm and unlink command
OS compatibility BSD Linux macOS Unix
Est. reading time 6 minutes
Advertisement

Syntax: rm command to remove a file

rm (short for remove) is a Unix / Linux command which is used to delete files from a filesystem. Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). The syntax is as follows to delete the specified files and directories:
$ rm {file-name}
$ rm [options] {file-name}
$ unlink {file-name}
$ rm -f -r {file-name}

Where,

  • -f: Forcefully remove file
  • -r: Remove the contents of directories recursively

When rm command used just with the file names, rm deletes all given files without confirmation by the user.

Warning: Be careful with filenames as Unix and Linux, by default, won’t prompt for confirmation before deleting files. Always keep verified backups of all critical files and data.

Unix Remove or delete a file example

Say you have a file named abc.txt and you want to remove it, type the following command and press the [Enter] key:
$ rm abc.txt
Verify that file is vanished from the file system using the ls command:
$ ls -l abc.txt
You will see following message:

ls: cannot access 'abc.txt': No such file or directory

Linux delete multiple files

Delete three files named foo.mp4, bar.doc, and demo.txt, run:
$ rm foo.mp4 bar.doc demo.txt
$ ls

Linux recursively delete all files

Remove all files and sub-directories from a directory (say deltree like command from MS-DOS world), enter:
$ rm -rf mydir

Linux delete a file and prompt before every removal

To request confirmation before attempting to remove each file pass the -i option to the rm command:
$ rm -i filename

Gif 01: rm command demo

Gif 01: rm command demo

Pass the -I option to prompt only once before removing more than three files but still providing protection against many mistakes at the cli:
$ rm -I foo.conf bar.conf resume.doc cakeday.png
$ rm -I -r -f ~/olddata/

Force rm command to explain what is being done with file

Pass the -v option as follows:
$ rm -v moiz.list.txt bios-updates.doc
Outputs:

removed 'moiz.list.txt'
removed 'bios-updates.doc'

How to delete empty directories

To remove empty directory use rmdir command and not the rm command:
$ rmdir mydirectory
$ rmdir dirNameHere
$ rmdir docs

How to read a list of all files to delete from a text file

The rm command is often used in conjunction with xargs to supply a list of files to delete. Create a file called file.txt:
$ cat file.txt
Add list of files to delete:

file1
/tmp/file2.txt
~/data.txt

Now delete all file listed in the file named file.txt, enter:
$ xargs rm < file.txt

How do I delete a file named -foo.txt or a directory named -bar?

To delete a file called -foo.txt:
$ rm -- -foo.txt
OR
$ rm -- ./-foo.txt
To delete a directory called -bar:
$ rm -r -f -- -bar
The two -- dashes tells rm command the end of the options and rest of the part is nothing but a file or directory name begins with a dash. See What Does ‐‐ (double dash) Mean In SSH Shell Command? for more info.

Never run rm -rf / as an administrator (root) or normal UNIX / Linux user

WARNING! These examples will delete all files on your computer if executed. Do not run them.
$ rm -rf /
$ rm -rf *
$ sudo rm -rf ./*

The rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf / variant of the command, if run by an administrator (root user), would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands.

Using wildcards (globbing) with rm command

A wildcard is nothing but a symbol to match an unknown character or set of characters. For instance, the asterisk ( * ) and the question mark ( ? ) are examples of a wildcard. Long ago, in UNIX V6, there was a program /etc/glob that would expand wildcard patterns. Soon afterward, this became a shell built-in. Thus, we can use wildcard matching for removing files in bulk.
Say I can type the following rm command. It will remove all files starting with a character such as abc.txt aaa.doc amazing_pic.jpg, and so on:
$ rm a*
Want to delete all files with .pl extension? Try
$ rm *.pl
The asterisk ( * ) represents any number of unknown characters and is very powerful. On the other hand, the question mark ( ? ) represents only one unknown character. For instance, to remove all files pattern such as aa, ab, ac, and so on, you can type:
$ rm a?
If you have deleted multiple files using wildcards, you can check the amount of free disk space on your Linux system by using either the "df" or "du command":
$ df -h
$ df -h /home/
$ du -csh /path/to/dir/

Please note that these wildcards are used by nearly any Linux or Unix-like system commands and are not limited to the rm. Be careful with wildcards, as you might end up deleting all or unwanted files. Do check wildcards using the "ls" before running the rm command. For example:
ls *.txt
Then if you see desired outputs or file you wish to remove, run:
rm *.txt

Conclusion

You learned how to delete files on Linux and Unix-like operating systems. Here are all important options for GNU rm command (read man page here)

Table 01: Remove files command summary
Option Description
-f Ignore nonexistent files and arguments, never prompt
-i Prompt before every file removal
-I Prompt once before removing more than three files, or
when removing recursively; less intrusive than -i, while still giving protection against
most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system when removing a hierarchy
recursively, skip any directory that is on a file system different from that of the
corresponding command line argument
--no-preserve-root do not treat '/' specially
--preserve-root[=all] do not remove '/' (default);
with 'all', reject any command line argument on a separate device from its parent
-r remove directories and their contents recursively
-R same as above
-d rmove empty directories
-v Explain what is being done
Do read the following man pages using the man command or help command:
$ man rm
$ man 7 glob
$ man 3 glob

See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

116 comments… add one
  • naveen Sep 6, 2014 @ 2:52

    How to delete file in linux

    • rajesh Feb 9, 2015 @ 12:20

      rm -f

  • Josiel de Assis Oct 13, 2014 @ 22:02

    How do I create a “Cron Job” on the Cpanel?
    I’d like to DELETE all files in the folder “infos”
    in directory “public_html” every Saturday 23:59.
    They all are in “mp3” format!

    Path:
    /public_html/infos

    How do I create the task in Cron on Cpanel to
    be performed in my website?

  • rajesh Feb 9, 2015 @ 12:19

    how to delete a file name with (-filename)

  • M.Altell May 9, 2016 @ 11:03

    How can i write script to delete all files and stay last week files only .

    for example :
    i need manual script to delete all files before 2/5/2016 and all files after 2/5 will not delete and this script will work weekly .

    Thanks

    • Mohammed Tabish Jun 27, 2016 @ 11:02

      hey M. Altell i think you mean to say that you want to delete files before 2/5/2016.

      just run this command,
      find . -not -newermt 2016-05-02 -exec rm -v {} \;

      Hope it helps :-)

  • Encognito Mar 16, 2017 @ 15:08

    Is there a way to search the entire file system and delete all files with ‘part of filename’ in all directories?

    • Anonymous Jun 24, 2023 @ 12:19

      To find all files ending with “.foo” or all files starting with “foo”, you can use the find command.

      a) First, you can use the -ls option to list all the files that match your criteria. This will help you to make sure that you are deleting the correct files.

      sudo find / -type f -name "foo*" -ls
      sudo find / -type f -name "*.foo" -ls
      

      b) If the results are correct, you can then use the -delete option to delete the files. Be careful when using this option, as it will delete the files permanently.

      ####
      #### Use command with caution 
      ####
      sudo find / -type f -name "foo*" -delete
      sudo find / -type f -name "*.foo" -delete
      

      The find command can be used to find files matching a specific criteria. The -ls option can be used to list all the files that match the criteria. The -delete option can be used to delete the files. Be careful when using the -delete option, as it will delete the files permanently.

  • Scott Edwards Oct 5, 2023 @ 17:16

    does rm only delete files in current directory? like if I use wildcard?

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Oct 5, 2023 @ 17:18

      You need to specify the full path for other dirs. For example, delete ‘*.tmp’ files in the /tmp/app1/ dir, run:

      rm /tmp/app1/*.tmp
      • Scott Edwards Oct 7, 2023 @ 23:42

        thanks for the quick response Vivek! Love the site and love the philosophy of user supported. Once I’m employed again plan on joining your Patreon.

  • Ketki Apr 1, 2024 @ 14:27

    Thank you for all examples. Useful on Amazon Linux AMI, too.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.