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.
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?
|Non-Linux FOSS: libnotify, OS X Style||Jun 18, 2013|
|Containers—Not Virtual Machines—Are the Future Cloud||Jun 17, 2013|
|Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer||Jun 12, 2013|
|Weechat, Irssi's Little Brother||Jun 11, 2013|
|One Tail Just Isn't Enough||Jun 07, 2013|
|Introduction to MapReduce with Hadoop on Linux||Jun 05, 2013|
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Introduction to MapReduce with Hadoop on Linux
- Validate an E-Mail Address with PHP, the Right Way
- RSS Feeds
- Tech Tip: Really Simple HTTP Server with Python
- New Products
- Weechat, Irssi's Little Brother
- Poul-Henning Kamp: welcome to
17 min 38 sec ago
- This has already been done
18 min 38 sec ago
- Reply to comment | Linux Journal
1 hour 3 min ago
- Welcome to 1998
1 hour 52 min ago
- notifier shortcomings
2 hours 16 min ago
3 hours 52 min ago
- Android User
3 hours 54 min ago
- Reply to comment | Linux Journal
5 hours 47 min ago
8 hours 37 min ago
- This is a good post. This
13 hours 50 min ago