The Linux 'rm' command (remove files and directories)

Linux FAQ: How do I delete files (remove files) on a Unix or Linux system?

The Linux rm command is used to remove files and directories. (As its name implies, this is a dangerous command, so be careful.)

Let's take a look at some rm command examples, starting from easy examples to more complicated examples.

How to delete files with rm

In its most basic use, the rm command can be used to remove one file, like this:

rm oldfile.txt

You can also use the rm command to delete multiple Linux files at one time, like this:

rm file1 file2 file3

If you prefer to be careful when deleting files, use the -i option with the rm command. The -i stands for "inquire", so when you use this option the rm command prompts you with a yes/no prompt before actually deleting your files:

rm -i files file2 file3

How to delete directories with rm

To delete Linux directories with the rm command, you have to specify the -r option, like this:

rm -r OldDirectory

The -r option means "recursive", and therefore this command recursively deletes all files and directories in the directory named OldDirectory.

As a warning, this command is obviously very dangerous, so be careful. Some people always add the inquire option when deleting directories, like this:

rm -ir OldDirectory

You can also delete multiple directories at one time, like this:

rm -r Directory1 Directory2 Directory3

How to use wildcards with rm

Unix and Linux systems have always supported wildcard characters, so in this case you can delete files and directories even faster. For instance, to delete all HTML files in the current directory, use this command:

rm *.html

Note that unlike DOS, you don't actually need the "." before the "html" in that command, so you can shorten the command like this:

rm *html

Unix and Linux wildcard characters and commands don't care at all about "." characters and filename extensions, so the "." is not needed.

You can also use wildcard characters in the middle of a filename or at the end of the filename. Here's an example where I'm deleting all files in the current directory that begin with the string "index":

rm index*

This command deletes files named index.html, index.php, and in general, any filename that begins with the character string "index".

You can also use wildcard characters like this to delete multiple files or directories:

rm Chapter[123].txt

That command deletes the files Chapter1.txt, Chapter2.txt, and Chapter3.txt, all in one command.

More Linux rm commands

There are probably many more Linux delete commands you can issue with the rm command. For instance, you can delete files and directories that aren't in the current directory. Here's an example where I delete a file named foo.txt that's in the /tmp directory:

rm /tmp/foo.txt

Summary

I hope these Linux rm command examples have been helpful. As usual, if you have any questions, or other good rm command examples, just leave a note in the Comments section below.