How to Delete Files on Linux

Linux has tons of commands that simplify tasks and make work more efficient. Rm is a helpful command for quickly deleting files, links, directories, etc. The word “rm” stands for remove, meaning you must use this command carefully as it is impossible to recover the files once you delete them with it. The command does not comprise any GUI trash bin, so create a backup of the files before deleting them with the rm. Here is the syntax of the rm command:

rm <options><filename>

Now Let’s look at the unique examples and understand everything about rm commands in Linux.

Useful Examples of rm Command in Linux

Before understanding examples of the rm command, here are some options that you can use in this command:

Option Description
-r (Recursive) With -r, you can delete all the contents of a specific directory with all the subdirectories.
-i (Interactive) With -i, you can get an interactive prompt before removing the file.
-f (Force) With -f, the system can remove files with no write access permission. Additionally, it suppresses helpful messages if a file doesn’t exist.
-v (Verbose) With -v, you can display the name of every removed file.

Let’s start with the simple example where we need to remove the file, which is in the Documents directory:

cd ~/Documents

The system will not show the confirmation prompt if you use the rm command for a regular file. So use the -i option to get a confirmation prompt in which you need to type ‘yes’ for removing a file:

rm -i

You can use the -f option with the rm command to remove a write-protected file. The -f option can easily bypass the access permission:

rm -f myvitux.txt

Use the -v option to get a confirmation message after successfully removing a file. This option displays a confirmation message of a file once it gets removed through the rm command:

rm -v

To remove a directory permanently, use the -r option with the rm command. Here is a simple example:

rm -r

rm has a -d option for removing empty directories. Here we are removing MyLinuxFile using the following command:

rm -d MyLinuxFile

For removing multiple files at once, add every file’s name in the command like this:

rm vitux.com1.txt vitux.com2.txt vitux.com3.txt

If the name of different files starts with the same word, then use the following command:


As you can see in the image above, we have deleted vitux1.txt, vitux.com2.txt, vitux.com3.txt, and vitux4.txt files using a single command.

If you want to delete all files of a current directory, then put * (asterisk) with the rm command in the terminal:

rm *

You can also use * to delete all files of a specific format or a specific name format. For example, let’s remove all txt files and remove all files with the particular name “”

rm *.txt

rm *

rm command gives a fantastic option by which you can remove many files by putting their name in a text file. So first, open the text file in the terminal using the cat command, and here we have opened the file:


Now use rm command with xargs command as xargs helps to access the file and perform the required action:

xargs rm <

You can’t use a simple rm command if a file name comprises dash (-). For example, if a filename is -vitux.txt, then you can’t use rm -vitux.txt as it will display an error like this:

Hence, you need to include the double dash (–) with rm command to delete that file:

rm -- -vitux.txt

Wrapping Up

This is how you can easily use rm commands in Linux to perform different tasks and remove files from the command line. We have explained 12 examples of rm commands that would be helpful for a Linux user.