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
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
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: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| 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 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
4 hours 11 min ago - Nice article, thanks for the
14 hours 51 min ago - I once had a better way I
20 hours 37 min ago - Not only you I too assumed
20 hours 55 min ago - another very interesting
22 hours 48 min ago - Reply to comment | Linux Journal
1 day 41 min ago - Reply to comment | Linux Journal
1 day 7 hours ago - Reply to comment | Linux Journal
1 day 7 hours ago - Favorite (and easily brute-forced) pw's
1 day 9 hours ago - Have you tried Boxen? It's a
1 day 15 hours 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