Command-line lifesavers

If you’re new to the command line, you’ve probably heard horror stories about people who’ve erased all their files with one command. And if you’re a Terminal veteran, you’ve undoubtedly had a few brushes with disaster. When you work with the command line, you work without a safety net—there’s no Trash to fish files out of and no Undo command. But there are some tricks for protecting yourself from missteps.

The erasers

Terminal is an amazingly efficient tool for deleting, moving, and copying files. One slip-up when you use these commands, though, and your files are gone forever.

rm Perhaps the most hazardous of commands is

rm
, the remove command. Omit a character or press return just a bit too quickly, and you can wipe out the wrong file, an entire folder full of files, or an entire hard drive.

Say you want to delete a file called Report from your desktop. You’d run the

rm
command, like so:

rm ~/Desktop/Report

Now say you have several files with names such as Report2005, Report2004, Report2003, and Report2002, and you want to delete the oldest one, using the command

rm ~/Desktop/Report2002
. Oops!—your fingers slip, your coffee spills, or your cat scratches you, and you accidentally type
Report2004
and press return. You’d better have a backup, because Report2004 is now an ex-file.

And that’s not the worst mistake you can make when you’re using

rm
. When you reference file and folder names made up of multiple words, Unix requires that you use either quotation marks (
rm ~/Desktop/"Report 2002"
) or a backslash (
rm ~/Desktop/Report\\ 2002
) to indicate that both words are part of the name. I recommend using quotation marks because it’s very easy to forget that backslash. For example, if you wanted to delete a file named Report 2002, you might accidentally type this:

rm ~/Desktop/Report 2002

Unfortunately, since you omitted the backslash, the

rm
command thinks that you’re telling it to delete two items, one called Report and the other called 2002. If you have a file named Report, it’s now gone without a trace.

cp and mv The

cp
command is Terminal’s equivalent of dragging a file in the Finder to another volume, or option-dragging a file (or selecting File: Duplicate) to copy it to a different location. Likewise, you use the
mv
command to move files from one place to another, much as you would drag a file to a different folder.

You run both the

cp
and
mv
commands like this:

command source destination

The source can be one file or several files. The destination can be a single file or a directory (folder)—for example:

cp Report2002 ~/Documents/Archives

Here I’ve copied the Report2002 file from the current working direc-tory to the Archives directory in my Documents folder. If you want to change the file’s name—to Report2002_copy, for instance—when you copy it, you can run this command:

cp Report2002 ~/Documents/Archives/ Report2002_copy

The danger of the

cp
command is not just that it replaces any existing file with the same name, but that it does so without warning you. So in the first example, if you already had a file in /Documents/Archives called Report2002, the
cp
command would simply overwrite that file with the new one. The Finder would warn you that you were about to replace an existing file, but Terminal assumes that you know what you’re doing.

Safety Measures Fortunately, the Unix founders foresaw the possibility that we users might get sleepy, distracted, or spaced out, and they added the

-i
, or interactive, option to give us a chance to prevent these mistakes. Even seasoned command-line mavens use the
-i
option to protect themselves from harmful typos. To use this option, add it to a command like this:

rm -i Report2002

Now Terminal will display a message—“remove Report2002?”—asking whether you’re sure you want to remove the file. You must type

yes
, or simply
y
, before the
rm
command does its work. If, however, you type anything else, such as
no
or
n
,
	or simply press return, you cancel the operation.
	

Both

cp
and
mv
also offer the interactive option, and using it whenever possible is a good idea. When you use this option, Terminal displays the following message if your command is going to replace an existing file: “overwrite file name and path ? (y/n [n])”. Press return to select the default answer, which is no. Type
y
or
yes
to tell Terminal to overwrite the file.

Add Automatic Protection If you know you’re forgetful, set up Terminal to use the interactive option automatically whenever you invoke

rm
,
cp
, or
mv
.

To do this, you must create a shell alias. Your shell (bash, by default) is the program that runs in Terminal whenever you launch the application. It goes without saying that a shell alias is nothing like a Finder alias. Rather, it’s a way of having one Unix command run another one.

You need to add your alias in the .bash_profile file in your Home folder. If you don’t already have such a file, use your favorite plain-text editor to create a file with that name. (See macworld.com/1110 for tips on using Bare Bones Software’s free TextWrangler.) Add the following lines:

	alias rm='rm -i'
	alias cp='cp -i'
	alias mv='mv -i'
	

When you save the file, the Finder will warn you that you’re creating an invisible file. To proceed, click on the Use ‘.’ button.

Essentially, you’ve created shortcuts for all these commands. Every time you type the

mv
command in Terminal, for example, no matter how complex the actual command is, the shell will run that command with the safer
-i
option, helping you avoid any unwanted deletion or overwriting. If there comes a time when you’re sure you want to nuke a lot of files, you can run any command in the normal manner, without answering Terminal’s questions for each one. To make Terminal ignore any alias you’ve set, run the
rm
command, for example, as follows:

\\rm file

The backslash tells the shell to use the real command, not its alias.

The redirection of no return

When the output of a command is long, or when you want to save it to review later, it’s convenient to save the output to a file instead of displaying it in Terminal. You do this by using the redirection character,

>
, followed by a space and then the destination file’s name.

For example, I can use the

sort
command to sort a list of phone numbers in a file, and then save the results in a file called sorted_phone.txt:

sort phone.txt > sorted_phone.txt

But you can run into problems if you forget to give your output file a new name. Overlook this simple step, and you’ll lose the original file.

Safety Measures Since redirecting output is such a common action (you may use it when you parse log files or run complex find commands), the Unix folk created a special variable, aptly named

noclobber
, that you can set in your shell. With this variable in place, Terminal will prevent you from overwriting files when you use the redirection character.

All you have to do is open your .bash_profile file and add one line:

set -o noclobber

Now, when you redirect output with the

>
character, you’ll see this error message: “cannot overwrite existing file.” This will save you from losing the original redirected data, or any other files with the same names as those you’ve chosen for redirection. All you need to do then is run your command again, renaming the output file, to save the output.

Play it safe

While the command line is a powerful way to perform many operations quickly and efficiently, Terminal does not warn you about the most common errors you can make. These simple protective measures can save your time, your files, and your peace of mind.

[ Kirk McElhearn is the author of The Mac OS X Command Line: Unix Under the Hood (Sybex, 2004). See his blog, Kirkville , for more tips about using OS X’s command line. ]

Geek favorites

Of Champagne and Soldering Irons: Are you looking for a geeky way to celebrate Apple’s 30th anniversary? Tom Owad is ready to send you back to school—the old school—in his book Apple I Replica Creation: Back to the Garage (Syngress, 2005). This hands-on guide steps you through soldering, assembling, and programming your own Apple I, just like the one Steve Wozniak introduced in April 1976 at the Homebrew Computer Club. Along the way, you’ll get a better understanding of what makes a computer tick. And you’ll get to immerse yourself in the computer that started it all.— Scholle Sawyer McFarland

Subscribe to the Create Newsletter

Comments