Linux rm command with Advance Syntax for Pro

The rm (remove) command is one of the essential Linux utilities that every basic to pro user should know, their working and usage.

What is the rm command in Linux?

The rm (remove) command is used to remove files and directories in the Linux system. This command is available by default in all Linux distributions.

It is a part of the GNU Coreutils, which includes other necessary commands (mv, cp, chmod, etc). But the question is, what happens when the rm command is executed?

Working of the rm command in Linux

Every time you execute the rm command, specifying the target file or directory (by default, it does not remove the directory unless the -r option is used), it will vanish without going into the trash.

The reason for this behaviour is that whenever you remove a file using the rm command, it will get unlinked from the file system without being shredded.

This will let other data overwrite the unlinked data block. That’s why, when you delete files in Linux, it is best not to perform any rewrite operation that will overwrite the data, making it hard to recover.

Be very careful while removing the file or directory using the rm command. It’s not like a Windows which lets you recover it from the recycle bin; once the file is gone, it’s gone.

Today, you will learn how to use the rm command to remove (unlink) files and directories like a pro in Linux.

Syntax

The rm command syntax consists of rm (defining the utility name), option (defining the type of operation to perform), and target (defining the file destination to take action), as shown below.

$ rm [OPTIONS] [TARGET DESTINATION]

Below is the list of the most commonly used options with the rm command.

rm * * *Removing Multiple Files
rm *.[extension]Find and remove the matching extension
rm — -*Remove target with a dash
rm -dRemove one or more empty directories
rm -rTo delete a target recursively
rm -fRemove a target forcefully
rm -iRemove a target interactively
rm -vRemove a target in verbose mode
Table of rm command options

Remove a file using the rm command

You can easily remove a file without specifying options using the rm command, as shown below.

$ rm file.txt

Below is the output of the above command.

Remove a file using the rm command
Remove a file using the rm command

As you can see, “file.txt” has been deleted from the current directory using the rm command. But what if the file is not located in the current directory?

In that case, you need to specify the exact location of the file you want to delete. For example, I have a file located in the /home/trendoceans/file.txt path. In this situation, I have to specify the exact location of the “file.txt” while using the rm command, as shown below.

$ rm /home/trendoceans/file.txt

Below is the output of the above command.

Remove a file from the exact location
Remove a file from the exact location

Removing Multiple Files using the rm command

If you have multiple files named “file1.txt“, “file.2.txt“, and “file3.txt” in your current path, then you can easily remove them by specifying all files together with the rm command, as shown below.

$ rm file1.txt file2.txt file3.txt

Below is the output of the above command.

Removing Multiple Files using the rm command
Removing Multiple Files using the rm command

Find and remove the matching extension using the rm command

If you have a directory full of files (*.jpg, *.png, *.txt, etc) and want to delete the files sharing the common extension “*.txt“, instead of specifying each file while deleting using the rm command.

You can easily remove all of them via their common extension, as shown below.

$ rm *.txt

Below is the output of the above command.

Find and remove the matching extension
Find and remove the matching extension

You can easily manage to delete other files sharing the matching extension. An example, rm *.txt *.jpg will remove all the text files and jpg images from the specified or current location.

Remove target with a dash using the rm command

Sometimes, people create files with illegal syntax, for example, file%2.txt or -file.txt. The special syntax used in between files can easily be deleted. What if you place “” at the beginning of the file name (-file.txt)?

This type of file will be treated as an option, which will make it difficult to delete. If you try to delete it, you will get an invalid syntax error.

To remove a file with “-” at the beginning of the file name, use “--” as an option or specify the file with “./“, as shown below.

$ rm -- -file.txt
OR 
$ rm ./file.txt

Below is the output of the above command.

Remove target with a dash
Remove target with a dash

Remove one or more empty directories using the rm command

If you only have an empty directory in your system, you can easily remove it with the “-d” option using the rm command, as shown below.

$ rm -d demodir/

Below is the output of the above command.

Remove one or more empty directories
Remove one or more empty directories

You can even remove multiple empty directories at the same time by specifying them all together as rm -d demodir1/ demodir2/ or you can even use the rmdir command as rmdir demodir1/ demodir2/ to delete null directories in the Linux system.

Remove one or more directories with content recursively using the rm command

Until now, we have learned how to remove directories without any content, but what if the directory holds the content inside it?

The standard way of using the rm command with the “-d” option will not do the job. If you try to remove it, you will get the “rm: cannot remove ‘xyz/’: Directory not empty” error, as shown below.

rm: cannot remove 'demodir/': Directory not empty
rm: cannot remove ‘demodir/’: Directory not empty

To remove such a directory containing content inside it, use the “-r” option to delete the directory recursively with all the content inside it, as shown below.

$ rm -r demodir/

Below is the output of the above command.

Remove one or more directories with content recursively
Remove one or more directories with content recursively

Remove a target forcefully using the rm command

We learned how to remove a directory containing multiple files inside. Sometimes, some files inside the directory do not have appropriate permission (write-protected) or are owned by another user.

This lack of permission will prompt you to decide on “y” or “n” while deleting the directory, as shown below.

Write protected file inside the directory
Write a protected file inside the directory

If you have made up your mind and want to delete everything forcefully without prompting yes or no, use the “-f” option with the “-r” option, as shown below.

$ rm -rf demodir/

Below is the output of the above command.

Remove a target forcefully
Remove a target forcefully

Remove a target interactively using the rm command

The rm command is very destructive, and so far, we have seen it not ask permission while deleting files. For the directory, if you were using the “-r” command and directory containing general files owned by you will be deleted in a splash of seconds.

If you want to interactively delete your files inside the directory by asking “y” or “n” on screen, use the “-i” flag as shown below.

$ rm -ri demodir/

Below is the output of the above command.

Remove a target interactively using the rm command
Remove a target interactively using the rm command

Remove a target in verbose mode using the rm command

Have you noticed until now that whenever you delete any directory containing files (ignore interactive way), it will not list the file being deleted on the terminal screen?

Use “-v” to enable the verbose mode to get the list of all files deleted inside the directory recursively on the terminal screen, as shown below.

$ rm -rv demodir/

Below is the output of the above command.

Remove a target in verbose mode using the rm command
Remove a target in verbose mode using the rm command

That’s all for now, Let us know in the comment section if you have any queries regarding this topic.

Leave a Reply