Lagos, Nigeria

How to Use “rm” in Linux – Technipages

The command line is the standard way to perform tasks in Linux. Some Linux devices, such as servers tend not to have any sort of graphic interface at all, requiring the use of commands to control it. Linux desktop computers tend to have a graphical interface to make them more accessible, but you generally don’t have the full functionality and versatility in configuration that you get from using commands in a terminal.

One of the standard commands that you should learn is “rm”. Short for “Remove”, “rm” allows you to delete files and, in certain circumstances directories too. “rm” is a risky command though, as it is easy to delete files that you didn’t necessarily mean to and potentially compromise the ability for your Linux installation to continue to function.

Running the command “rm–help” will show a help page.

What not to do

Given the power of “rm”, it’s a good idea to make sure that you understand what you shouldn’t do before you try to use it. The “-r” flag is used to apply the deletion recursively to all directories in the specified directory. This allows you to delete a directory and everything in it. The “-f” flag is used to force the deletion and will not prompt for the confirmation of the deletion of important files. Combined these two flags “-rf” can allow you to delete entire sets of directories with no confirmation prompts.

Tip: As with many commands, you can combine command-line flags such as “-r -f” into “-rf”. The order of the flags doesn’t matter, so “-fr” will do the same thing.

In the worst-case scenario, you could accidentally run the command on the root directory. The command would recursively delete everything from your hard drive that your user has permissions to edit and completely mess up your Linux installation. To protect you from the absolute worst-case scenario, the root directory “/” is treated differently by “rm”. You would need to use “sudo” and the flag “–no-preserve-root” to delete everything, this runs the command with root permissions and removes protections for the root directory respectively.

Note: You should never try combining the “rm” command with the “-rf” flags and pointing the command at the root directory unless you are 100% confident that you will never need the system again.

Tip: The double-dash at the start of “–no-preserve-root” is intentional. It is standard Linux command flag syntax to have single-letter flags use a single dash and to have multi-letter flags start with a double dash. For example, the “-h” flag is often, but not always, used to open a help page, while the “–help” flag generally also does the same. The double dash helps to differentiate a multi-letter flag from a combination of multiple single-letter flags such as “-h -e -l -p”.

Be very careful when using the “-rf” flags with the “rm” command, especially when using Sudo permissions. You should never need to use the –no-preserve-root command. Ideally, every time before you run the “rm” command you should double-check that you’ve not made a typo.

The wildcard character “*” is also dangerous to use with the “rm” command as it will select everything in the current directory, with the exception of other directories unless “-r” is specified.

How to use “rm”

To use “rm” safely, you should always double-check your command to make sure that you haven’t made a typo or autocompleted to the wrong filename. Ideally, you should only delete one file at a time, but if you’re careful, using the wildcard “*” to select multiple files can work too. For example, the command “rm *.txt” would remove all files in the current directory that end in “.txt”.

The command “rm *.txt” uses the wildcard character to match and delete all files that end in .txt

The similar command “rmdir” is a safer way to delete directories because if you don’t specify any flags it can only delete empty directories. The use of “rm” and “rmdir” prevents you from accidentally recursively deleting data.

The command “rm -r” is needed to delete a directory and its contents, you should be sure that you want to delete the contents though.

This post was written by Mel Hawthorne and was first posted to Technipages

Do you find this article helpful? Your Friend might too. So, please Share it with them using the Share button above.
Will you like to get notified when I post new updates? Then Follow me on any of my social media handles: Google News, Telegram, WhatsApp, Twitter, Facebook, Pinterest.
You can also drop your email address below if you wish to be notified by mail.

Tags: , ,

%d bloggers like this: