16.4. Online Help

 <  Day Day Up  >  

16.3. Working with Files and Directories

The previous pages show you how to navigate your directories using Unix commands. Just perusing your directories isn't particularly productive, however. This section shows you how to do something with the files you see listed ”copy, move, create, and delete directories and files.


Tip: You're entering Serious Power territory, where it's theoretically possible to delete a whole directory with a single typo. As a precaution, consider working through this section with administrator privileges turned off for your account (Section 12.1.2), so that you won't be able to change anything outside your home directory ”or to be really safe, create a new, test account just for this exercise so even your personal files won't be at risk.

16.3.1. cp (copy)

Using the Unix command cp , you can copy and rename a file in one move. (Try that in the Finder!)

The basic command goes like this: cp path1 path2 , where the path placeholders represent the original file and the copy, respectively.

FREQUENTLY ASKED QUESTION
The Slash and the Colon

OK, I'm so confused . You say that slashes denote nested directories. But in Mac OS 9, I always read that colons (:) denote the Mac's internal folder notation, and that's why I can't use colons in the names of my icons. What's the story?

At the desktop, the Mac still uses colons as path separators instead of the slash. Therefore, you are allowed to use slashes in file names in the Finder, but not a colon. Conversely, in Terminal, you can use colons in file names but not slashes!

Behind the scenes, Mac OS X automatically converts one form of punctuation to the other, as necessary. For example, a file named Letter 6/21/2002 in the Finder shows up as Letter 6:21:2002 in Terminal. Likewise, a directory named Attn: Jon


16.3.1.1. Copying in place

To duplicate a file called Thesis.doc, you would type cp Thesis.doc Thesis2.doc . (That's just a space between the names.) You don't have to call the copy Thesis2 ”you could call it anything you like. The point is that you wind up with two identical files in the same directory with different names. Just remember to add a backslash before a space if you want to name the copy with two words ( Thesis\ Backup , for example).


Tip: If this command doesn't seem to work, remember that you must type the full names of the files you're moving ”including their file name suffixes like .doc or .gif, which Mac OS X usually hides. Using the ls command before a copy may help you find out what the correct, full file names should be. Or you may just want to use the tab-completion feature, making Terminal type the whole name for you.
16.3.1.2. Copying and renaming

To copy the same file into, say, your Documents folder instead, just change the last phrase so that it specifies the path, like this: cp Reviews.doc ~/Documents/Reviews2. doc .

Figure 16-6. The first argument of this command lists two different files. The final clause always specifies where you want all of these files moved to (in this case, the FinishedMovies directory).


WORKAROUND WORKSHOP
When Forks Collide: CpMac and MvMac

If you could inspect certain traditional Macintosh files with the software equivalent of a CAT scanner, you'd see that its single icon might actually house two chunks of computer code, which the geeks call the data fork and the resource fork . (Data refers to, well, data ”stuff you type, or the programming code that makes up an application. Resources are programming components like icons, windows , toolbars , and other pieces of your applications.)

This fork business is news to almost all flavors of Unix, however, including all versions of Mac OS X before Tiger. When you use the cp command included with those operating systems (or mv , described later in this chapter), you copy or move only the data fork ”the one those OSes know about. The effect on the copied or moved file can be minor or disastrous, depending on the type of file. Many non-Cocoa programs (AppleWorks, Word, and so on), for example, are rendered useless, having lost many of their essential resources. Many also end up with blank icons, having lost their type and creator codes (Section 5.5.1).

But ring them bells and start the parade: In Tiger, cp and mv are resource-fork aware. You can use them with any file on your Mac, and they do the right thing.



Tip: Note that cp replaces identically named files without warning. Use the -i flag (that is, cp -i ) if you want to be warned before cp replaces a file like this.
16.3.1.3. Copying without renaming

To copy something into another directory without changing its name, just use a pathname (without a file name) as the final phrase. So to copy Reviews.doc into your Documents folder, for example, you would type cp Reviews.doc ~/Documents .


Tip: You can use the "." directory shortcut (which stands for the current working directory) to copy files from another directory into the working directory, like this: $ cp ~/Documents/Reviews.doc . (Notice the space and the period after Reviews.doc .)
16.3.1.4. Multiple files

You can even move several files or directories at once. Where you'd normally specify the source file, just list their pathnames separated by spaces, as shown in Figure 16-6.

You can also use the * wildcard to copy several files at once. For example, suppose you've got these files in your iMovie Projects directory: Tahoe 1.mov, Tahoe 2.mov, Tahoe 3.mov, Tahoe 4.mov, Script.doc, and Tahoe Project File. Now suppose you want to copy only the QuickTime movies into a directory called FinishedMovies. All you'd have to do is type cp *mov ../FinishedMovies and press Enter; Mac OS X instantly performs the copy.

If you wanted to copy all of those files (not just the movies) to another directory, you'd use the * by itself, like this: cp * ../Finished Movies .

Unfortunately, if the iMovie Projects directory contains other directories and not just files, that command produces an error message. The Unix cp command doesn't copy directories within directories unless you explicitly tell it to, using the -R option flag. Here's the finished command that copies everything in the current directory ”both files and directories ”into FinishedMovies: cp -R * ../FinishedMovies .

Here's one more example: a command that copies everything (files and directories) with Tahoe in its name into someone else's Drop Box directory: cp -R *Tahoe* ~miho/Public/Drop\ Box .

16.3.2. mv : Moving and Renaming Files and Directories

Now that you know how to copy files, you may want to move or rename them. To do so, you use the Unix command mv almost exactly the same way you'd use cp (except that it always moves directories inside of directories you're moving, so you don't have to type -R ).

The syntax looks like this: mv oldname newname. For example, to change your Movies directory's name to Films, you'd type mv Movies Films . You can rename both files and directories this way.

16.3.2.1. Moving files and directories

To rename a file and move it to a different directory simultaneously , just replace the last portion of the command with a pathname. To move the Tahoe1 movie file into your Documents directory ”and rename it LakeTahoe at the same time ”type this: mv Tahoe1.mov ~/Documents/LakeTahoe.mov .

All the usual shortcuts apply, including the wildcard. Here's how you'd move everything containing the word Tahoe in your working directory (files and directories) into your Documents directory: mv *Tahoe* ~/Documents.

16.3.2.2. Option flags

You can follow the mv command with any of these options:

  • -i . Makes Terminal ask your permission before replacing a file with one of the same name.

  • -f . Overwrites like-named files without asking you first. (Actually, this is how mv works if you don't specify otherwise .)

  • -n . Doesn't overwrite like-named files; just skips them without prompting.

  • -v . Displays verbose (fully explained) explanations on the screen, letting you know exactly what got moved.


Tip: If you use a combination of options that appear to contradict each other ”like the -f, -i, and -n options ”the last option (farthest to the right) wins.

By the way, the mv command never replaces a directory with an identically named file . It copies everything else you've asked for, but it skips files that would otherwise wipe out folders.

16.3.3. mkdir : Creating New Directories

In the Finder, you make a new folder by choosing File New Folder. In Terminal, you create one using the mkdir command (for make directory ).

Follow the command with the name you want to give the new directory, like this: mkdir 'Early iMovie Attempts' (the single quotes in this example let you avoid having to precede each space with a backslash).

The mkdir command creates the new directory in the current working directory, although you can just as easily create it anywhere else. Just add the pathname to your argument. To make a new directory in your Documents Finished directory, for example, type mkdir '~/Documents/Finished/Early iMovie Attempts' . Thanks to Tiger's Spotlight technology, the new directory appears immediately in the Finder. (It used to take a few moments for it to appear.)


Tip: If there is no directory called Finished in your Documents directory, you just get an error message ”unless you use the -p option, which creates as many new directories as necessary to match your command. For example, mkdir -p '~/Documents/Finished/Early iMovie Attempts' would create both a Finished directory and an Early iMovie Attempts directory inside of it.

16.3.4. touch : Creating Empty Files

To create a new, empty file, type touch filename . For example, to create the file practice.txt in your working directory, use touch practice.txt .

And why would you bother? For the moment, you'd use such new, empty files primarily as targets for practicing the next command.

16.3.5. rm : Removing Files and Directories

Unix provides an extremely efficient way to trash files and directories. With a single command, rm , you can delete any file or directory ”or all of those that you're allowed to access with your account type.

The dangers of this setup should be obvious, especially in light of the fact that deletions are immediate in Unix. There is no Undo, no Empty Trash command, no "Are you sure?" dialog box. In Unix, all sales are final.

The command rm stands for "remove," but could also stand for "respect me." Pause for a moment whenever you're about to invoke it. For the purpose of this introduction to rm , double-check that administration privileges are indeed turned off for your account.

To use this command, just type rm , a space, and the exact name of the file you want to delete from the working directory. To remove the file practice.txt you created with the touch command, for example, you'd just type rm practice.txt .

To remove a directory and everything in it, add the -r flag, like this: rm -r Practice-Folder .

If you're feeling particularly powerful (and you like taking risks), you can even use wildcards with the rm command. Now, many experienced Unix users make it a rule to never use rm with wildcards while logged in as an administrator, because one false keystroke can wipe out everything in a directory. But here, for study purposes only, is the atomic bomb of command lines, the one that deletes everything in the working directory: rm -rf * .


Tip: Be doubly cautious when using wildcards in rm command lines, and triply cautious when using them while logged in as an administrator.

Just after the letters rm, you can insert options like these:

  • -d deletes any empty directories it finds, in addition to files. (Otherwise, empty directories trigger an error message.)

  • -f squelches any attempts to get your attention, even if there's a problem, like a permission error or nonexistent file. The command proceeds, full speed ahead.

  • -i (for interactive ) makes the Mac ask for confirmation before each deletion.

16.3.6. srm : Secure Removal

Behind the scenes, the Finder's new Secure Empty Trash function (Section 2.7.4) actually triggers Unix's srm (secure removal) tool. Of course, you can also use srm directly from the command line. One benefit: In Terminal, you can control just how thoroughly Mac OS X scrubs the hard drive spot where the deleted file once sat.

The srm utility lets you specify three general levels of deletion:

  • Simple . The -s flag tells srm to perform a simple secure removal, overwriting the deleted material with random data just once. It's faster than the Finder's Secure Empty Trash, but not as thorough.

  • Medium . The -m flag designates medium level, which overwrites the unwanted data seven times with various types of random and not-so-random data. This is what you get when you use the Finder's Secure Empty Trash command, and it's thorough enough to meet U.S. Department of Defense security requirements.

  • Strong . If you don't specify either -s or “m, srm will perform a strong secure removal. That entails recording over the spot where the deleted file sat 35 times , each time using a different string of data as specified by the Gutmann algorithm. (And what is the Gutmann algorithm? A series of data patterns that make recovery of an erased file almost impossible . More than you ever wanted to know is at www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html.)

    The bottom line: To make sure no one ever, ever reads that poem you typed out for your cat one lonely , bleary-eyed evening, type srm "My Twinkie.doc" . That will be the end of it, and neither the CIA nor Norton Utilities will ever know what it was.

16.3.6.1. echo: The Safety Net

You can make rm or srm less risky by prefacing it with the echo command. It makes Terminal type out the command a second time, this time with a handy list of exactly what you're about to obliterate. If you've used wildcards, you see the names of the files that will be affected by the * character. If you type echo rm -r * , for example (which, without the echo part, would normally mean "delete everything in this directory"), you might see a list like this:

 rm -r Reviews.doc Tahoe Footage  Picnic Movie Contract.doc 

Once you've reviewed the list and approved what Terminal is about to do, then you can retype the command without the echo portion.


Note: The rm command doesn't work on file or directory names that begin with a hyphen (-). To delete these items from your working directory, preface their names with a dot slash (./), like this: rm ./-Recipes.doc .
 <  Day Day Up  >  


Mac OS X. The Missing Manual
Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
ISBN: 0596153287
EAN: 2147483647
Year: 2005
Pages: 506
Authors: David Pogue

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net