Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
< Day Day Up > |
17.4. Where to Go from Here
This crash course in Unix should give you a first helping of guidance ”enough to perform some genuinely useful tasks in Mac OS X, and to acquire a feeling for how Unix commands work. There's a lot more to learn, however. As you peruse beginner-level Unix books and Web sites (see Appendix E), for example, you'll gradually become familiar with a variety of important terms and tools. 17.4.1. Aliases
Don't be confused : Aliases in Unix have nothing to do with traditional Macintosh icon aliases. Instead, Unix aliases are more like text macros, in that they're longish commands that you can trigger by typing a much shorter abbreviation.
For example, remember the command for unlocking all the files in a folder? (It was sudo chflags -R nouchg [pathname] . To unlock everything in your account's Trash, for example, you'd type sudo chflags -R nouchg ~/.trash .) Using the alias command, however, you can create a much shorter command ( unlock , for example) that has the same effect. (The alias command takes two arguments: the alias name you want, and the command it's supposed to type out, like this: alias unlock='sudo chflags -R nouchg ~/.trash' .) On the down side, aliases you create this way linger in Terminal's memory only while you're still in the original Terminal window. As soon as you close it, you lose your aliases. When you get better at Unix, therefore, you can learn to create a .bash_profile or .bashrcfile that permanently stores all your command aliases. (Hint: Open or create a file called .bash_profile in your home directory, and add to it one alias command per line.) 17.4.1.1. nano, emacs, vim
One way to create and edit text files containing aliases (and to perform other command- related tasks) is to use nano , a popular text editor that's an improved version of the pico editor that shipped with Panther (see Figure 17-4). In fact, if you try to run pico, nano opens instead. As you'll discover just by typing nano and pressing Enter, nano is a full-screen Unix application. You enter text in nano much as you do in TextEdit or SimpleText, yet nano is filled with features that are especially tailored to working with Unix tasks and commands.
Nor is nano the only text editor that's built into the Unix under Mac OS X. Some Unix fans prefer the more powerful and complex vim or emacs , in the same way that some people prefer Microsoft Word to TextEdit. 17.4.1.2. date
Used all by itself, the date command simply displays the current date and time. However, you can use its long list of date "conversion specifications" (enter man date to see all of them) to format the date string in any conceivable way. Begin the string with a + and then enter the formatting you like, mixing in any regular text as well, like this: office-mac:~ chris$ date +"I can t be- lieve it's already week %V of %Y, here in the %Z time zone. But what do you expect on a %A at%l:%M %p?"
can't believe its already week 29 of 2005, here in the PDT time zone. But what do you expect on a Saturday at 1: 37 PM? Note: Be careful about using date with sudo . If you do, and accidentally forget the leading +, you reset your Mac's built-in clock. 17.4.1.3. grep
The grep program is a filter , a powerful program designed to search data for text that matches a specified pattern. It can pass on the processed result to another program, file, or the command line itself. You specify the text pattern you want grep to search using a notation called regular expressions. For example, the regular expression dis[ck] searches for either disk or disc . Another example: To search for lines in a file containing the addresses from 200 to 299 Redwood Way, you could tell grep that you're looking for "\<2[0-9][0-9] Redwood Way" . One terrific thing about grep is that its search material can be part of any file, especially plain text files. The text files on your Mac include HTML files, log files, and ”possibly juiciest of all ”your email mailbox files. Using grep , for example, you could search all your Mail files for messages matching certain criteria, with great efficiency and even finer control than with Spotlight. 17.4.1.4. find
Now that Spotlight's on the scene, you might wonder why you would need to use the Unix find command. Well, for one, find takes file searching to a whole new level. For example, you can find files based on their permissions, owner name, flag settings, and of course any kind of name pattern you can think of using regular expressions. Also, like most other Unix commands, you can "pipe" the find command's list of found files straight into another program for further processing. You might do this to change their names , convert them to other formats, or even upload them to a network server. Perhaps best of all, since you can run find with sudo , you can look for files existing anywhere on your hard disk, regardless of directory permission settings. To find all files in your Home directory with "Bolinas" in their names, for example, you would use this command: find ~/ -name '*Bolinas*'
Or, to ignore capitalization: find ~/ -iname '*Bolinas*'
And this command searches for all the locked files in your Home directory: find ~/ -flags uchg
17.4.1.5. launchd
launchd is a multitalented Unix program that's responsible for launching system programs, during startup or any time thereafter. Part of its job is triggering certain commands according to a specified schedule, even when you're not logged in. People can use launchd to trigger daily backups or monthly maintenance, for example. You can program your unattended software robot by editing simple property list files. Mac OS X comes set up to run launchd automatically; it's the very first process that starts up when the Mac does. It launches all your other startup items, in fact. (If you open the Activity Monitor program in your Applications Utilities folder, you'll see it listed among the administrator processes that your Mac is running all the time.) In fact, launchd comes with three under-the-hood Unix maintenance tasks already scheduled: a daily job, a weekly job, and a monthly job. They come set to run at 3:15 a.m., 4:30 a.m., and 5:30 a.m. If your Mac isn't generally turned on in the middle of the night, these healthy jobs may never run. You can either leave your Mac on overnight every now and then or, if you're feeling ambitious, change the time for them to be run. A glance at man launchd. plist shows you how. (Hint: It involves using sudo nano and editing the three com.apple.periodic property list files in /System/Library/LaunchDaemons ”but be careful not to mess with anything else in there!) Note: Before Tiger, Mac OS X used the cron utility to run these jobs. cron still exists on Tiger and will start working as it always has ”the minute you add a new cron job. See the cron and crontab man pages for details. 17.4.1.6. curl
With curl , all you need is a file's URL, and you can download it from an FTP site or Web server, without launching an FTP program (or enduring a single pop-up ad). To download the entire "Missing CD" for the second edition of this book (see the inside back cover), just type this: curl -O http://examples.oreilly.com/ macosxmm2/macosxmm2_all.zip
When the download is complete, you see a note in the Terminal window, and the downloaded file appears right there in your working directory. You can also use this command to upload a file, using the -T flag. To add something to the Incoming folder of an anonymous FTP site, for example, use this command line: curl -T BigBasin.sit ftp://anonymous@ ftp.coast-photo.com/Incoming
In this case, the name of the file you're uploading is BigBasin.sit, and the FTP site is ftp.coast-photo.com. You'll be asked for a password (for anonymous access it's, by convention, your email address) and then the upload begins. 17.4.1.7. sips
sips (Scriptable Image Processing System), an Apple Unix utility included with Tiger, lets you process graphics files like TIFF, JPEG, and GIF files from within Terminal. You can use sips to get more information about such files (type, size , or attached ColorSync profile), for example, or to remove or attach ColorSync profiles. Better still, sips can modify the images by scaling, rotating, flipping, or converting them to other formats (like PNG, Photoshop, or PICT). You can also generate custom Finder icons for image files without them. Though there's no manpage for sips , you can view its two help screens to learn more. Type sips -h to get general help, and sips -H to see the list of image file properties that sips can work with. For example, to flip a digital photo called TenLakes.jpg vertically (and save the flipped version with the name TenLakesFlipped.jpg ), type this: sips -f vertical TenLakes.jpg out Ten- LakesFlipped.jpg
(If you fail to include the ” out option and a new filename, sips permanently transforms the original image file instead of spinning out a new one.) You can even include multiple sips actions in one command. So, for example, to make the same flip but also transform the file into a TIFF file, enter this: sips -f vertical -s format tif Ten- Lakes.jpg out TenLakesFlipped.tif
17.4.1.8. telnet, ftp
Telnet and FTP aren't exclusively Unix programs, of course. Techies from all walks of operating-system life have used telnet for years whenever they want to tap into another computer from afar, and FTP to deliver and download software files. Details on both are in Chapter 22. |
< Day Day Up > |