The rm Command
The rm command is probably one of the first commands you learned. Here we look at some options that may save you a lot of time. Before we get into the details, some words of warning. In the Unix tradition, Linux does not ask unnecessary questions. If you tell it to remove a file or a set of files, it will do just that. If you want it to ask you for confirmation, you will need to ask it to do that.
The basic syntax of rm is:
rm [options] filenames
The options must start with a -. One or more filenames can be specified, and wildcards are permitted (because the shell, not rm, expands them).
If you are not familiar with wildcards (* and ? to name the most dangerous), read up on them. Placing a wildcard character in the file name list by accident can make you a very unhappy camper.
rm is the command used, in Linux terminology, to unlink a file. What this means is that the directory entry for the file is removed. A side effect (and the effect that we generally expect) is that the file is deleted. But this may not be the case.
The Linux file system makes it possible for a file to have more than one name or directory entry. The ln command allows you to create these additional names or links. If these links are hard links, links created with the ln command without the -s option, you have a file that can be accessed by these multiple names.
By using the rm command on one of these names, you only delete the name, not the actual file. When the last name pointing to the file is removed, the file is finally removed.
Now that you know about the basics, there are a bunch of options that make it possible to do more than just remove a file. A handy option for the timid is -i. The -i stands for interactive. When specified, rm will prompt you before it deletes each file. If you respond with y or Y the file will be deleted, otherwise the delete will be skipped.
For example, if you enter:
rm -i dog cat pig
you will be prompted with:
rm: remove `dog'?
Pressing y or Y and <return> will cause the file dog to be deleted. No matter what you pressed, rm will then move along to the next file, in this case cat, and prompt again.
Normally, if rm encounters a file that you do not have write permission to, but you do have permission to modify the file's directory, it will ask for confirmation. You then enter y or Y followed by <return> to force the removal of the file. The -f option overrides this default behavior. If you specify -f, rm will do the removal without the prompt. This option also eliminates the error message that rm generally produces if a specified file is not found.
Now, the scary option, -r. The -r stands for recursive. If you specify a directory name and the -r option, rm will remove the specified directory and all its contents, including any subdirectories contained within it (and the subdirectories' files and subdirectories and so forth). For example, if you had a directory named Joe in your current directory which contained the files name address phone and a directory Other that contained the files ssn and age, you could delete each file individually with the following command:
rm Joe\name Joe\address Joe\phone Joe\Other\ssn\ Joe\Other\age
You could then use the rmdir command to remove the directories Other and Joe:
rmdir Joe/Other Joe
the command:
rm -r Joe
Finally, a trick. A common problem people run into is how to delete a file whose name starts with a -. For example, if you entered the command
rm -garbagefile
in an attempt to remove a file named -garbagefile, you would get the error message:
rm: illegal option -g
Try rm -help for more information.
This is because rm assumes that if its first argument starts with a - it is an option. The solution is to use a name that does not confuse rm. For example, you can use either the full pathname of the file or a relative pathname where you explicitly specify the current directory using ./. Thus, the following command would do the job:
rm ./-garbagefile
Phil Hughes is the publisher of Linux Journal.
Phil Hughes
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Trying to Tame the Tablet
- New Products
- Ahh, the Koolaid.
7 min 44 sec ago - git-annex assistant
6 hours 7 min ago - direct cable connection
6 hours 29 min ago - Agreed on AirDroid. With my
6 hours 40 min ago - I just learned this
6 hours 44 min ago - enterprise
7 hours 14 min ago - not living upto the mobile revolution
10 hours 5 min ago - Deceptive Advertising and
10 hours 41 min ago - Let\'s declare that you have
10 hours 42 min ago - Alterations in Contest Due
10 hours 43 min ago




Comments
found a genuine bug in the rm command
Phil (or whoever)
Who do I contact about a bug in the rm command? It needs to be fixed, because the bug creates a situation with unerasable files on the hard disk.
Randall