• Skip to main content
  • Skip to primary sidebar

Technipages

Smart phone, gadget and computer tutorials

  • Topics
    • Android
    • Gaming
    • Hardware
    • Internet
    • iOS
    • MacOS
    • Office
    • Software
    • Windows
    • Definitions
  • Product Reviews
  • Downloads
  • About
How to Use “rm” in Linux

How to Use “rm” in Linux

Posted on November 14, 2020 by Mel Hawthorne Leave a Comment

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.

You Might Also Like

  • What's the Best VPN for Linux
    What's the Best VPN for Linux
  • Top 5 Linux Distributions You Should Try
    Top 5 Linux Distributions You Should Try
  • How Does Chown Work in Linux?
    How Does Chown Work in Linux?
  • How to Pull a Git Repository in Linux
    How to Pull a Git Repository in Linux
  • How to Push a Change to Git in Linux
    How to Push a Change to Git in Linux
  • How to Clone a Git Repository in Linux
    How to Clone a Git Repository in Linux
  • Linux Mint: How to Add New Applets
    Linux Mint: How to Add New Applets
  • Linux Mint: How to Add New Extensions
    Linux Mint: How to Add New Extensions
  • Linux Mint: How to Use “Hot Corners”
    Linux Mint: How to Use “Hot Corners”

Filed Under: Software

Reader Interactions

Did this help? Let us know! Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Recent Posts

  • Bitwarden: How to Manually Autofill a Login Form
  • Bitwarden: How to Copy the Username and Password of an Entry
  • Bitwarden: How to Open the URL Associated with an Entry
  • How to Increase the Size of Text on Any Website on iPad
  • Chrome: How to Fix Can’t Scroll Using Scrollbars
  • Samsung Galaxy S 21 Plus: How to Check How Much Memory Is Left
  • Google Photos: How to Export Your Albums
  • Bitwarden: How to Deauthorize All Sessions

Who’s Behind Technipages?

Baby and Daddy My name is Mitch Bartlett. I've been working in technology for over 20 years in a wide range of tech jobs from Tech Support to Software Testing. I started this site as a technical guide for myself and it has grown into what I hope is a useful reference for all.

Follow me on Twitter, or visit my personal blog.

You May Also Like

  • Linux

© Copyright 2021 Technipages · All Rights Reserved · Privacy