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

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 vitux.com.txt file, which is in the Documents directory:

cd ~/Documents
ls
rm vitux.com.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-1.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="162" src="data:image/svg xml,” width=”757″>

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:

ls
rm -i vitux.com.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-2.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="77" loading="lazy" src="data:image/svg xml,” width=”702″>

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:

ls
rm -f myvitux.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-3.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="97" loading="lazy" src="data:image/svg xml,” width=”727″>

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 vitux.com.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-4.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="81" loading="lazy" src="data:image/svg xml,” width=”777″>

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

ls
rm -r vitux.com

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-5.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="92" loading="lazy" src="data:image/svg xml,” width=”732″>

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

rm -d MyLinuxFile

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-6.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="72" loading="lazy" src="data:image/svg xml,” width=”726″>

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

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-7.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="110" loading="lazy" src="data:image/svg xml,” width=”736″>

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

ls
rm vitux.com[1234].txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-8.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="72" loading="lazy" src="data:image/svg xml,” width=”738″>

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:

ls
rm *

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-9.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="217" loading="lazy" src="data:image/svg xml,” width=”727″>

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 “vitux.com”

ls 
rm *.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-10.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="252" loading="lazy" src="data:image/svg xml,” width=”702″>

ls 
rm *vitux.com

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-11.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="93" loading="lazy" src="data:image/svg xml,” width=”742″>

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 vitux.com.txt file:

cat vitux.com.txt

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

xargs rm < vitux.com.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-12.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="112" loading="lazy" src="data:image/svg xml,” width=”726″>

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:

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-13.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="115" loading="lazy" src="data:image/svg xml,” width=”732″>

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

rm -- -vitux.txt

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2023/03/echo/word-image-19668-14.png" data-ez decoding="async" ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="72" loading="lazy" src="data:image/svg xml,” width=”732″>

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.