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
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
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:
Phil Hughes is the publisher of Linux Journal.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Peppermint 7 Released
- Sony Settles in Linux Battle
- Libarchive Security Flaw Discovered
- Maru OS Brings Debian to Your Phone
- Profiles and RC Files
- Snappy Moves to New Platforms
- The Giant Zero, Part 0.x
- Git 2.9 Released
- Susan Lauber's Linux Command Line Complete Video Course (Prentice Hall)
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide