< Day Day Up > |
16.3. Working with Files and DirectoriesThe 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.
16.3.1.1. Copying in placeTo 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 renamingTo 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 .
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 renamingTo 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 filesYou 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 DirectoriesNow 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 directoriesTo 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 flagsYou can follow the mv command with any of these options:
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 DirectoriesIn 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 FilesTo 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 DirectoriesUnix 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:
16.3.6. srm : Secure RemovalBehind 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:
16.3.6.1. echo: The Safety NetYou 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 > |