Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
< Day Day Up > |
17.5. Putting It Together
All of the Unix syntax and vocabulary presented in this chapter and Chapter 16 is all well and good, and it'll give you the rosy glow of having mastered something new. But it still doesn't entirely explain why Unix gives programmers sweaty palms and dilated pupils. The real power of Unix comes farther down the road ”when you start stringing these commands together. Suppose, for example, you want to round up all the TIFF image files related to your Yosemite project, scale them to a common size , convert them to JPEG files, and copy them to an FTP site. How would you go about it? You could, of course, use Spotlight to search for all TIFF files that have "Yosemite" in their names. But what if your images were named otherwise but kept in folders with Yosemite in their names ? You would have to find those folders first, and then the TIFF files within them. You could perform the next step (scaling and converting the image) either manually or by a preprogrammed script, using a program like Photoshop or even iPhoto. Once the images were all done, you'd need to collect them and then use your favorite FTP program to upload them to the server. If you've mastered Unix, though, you could shave 12 minutes off of your workday just by changing to an empty working directory and typing this as one long line: find ~ -type f -ipath '*yosemite*tif' -print0 xargs -0 sips -Z 250 -s for- mat jpeg out . ; ftp -u ftp://ftp. coast-photo.com/Incoming *
Even after two chapters of Unix basics, that mass of commands probably looks a tad intimidating. And, indeed, if you've never programmed before, even the following breakdown may make your eyes glaze over. Even so, pieces of it should now look familiar:
Note: As written, this command works only if you don't need a password to get into the FTP site. Otherwise, build your FTP account name and password into its address like this: ftp://chris:password@ftp.coast-photo.com/Incoming. When you press Return or Enter after this gigantic command, Mac OS X scans all the directories inside your Home directory, rounds up all the Yosemite-related images, scales them, converts and renames them, and then uploads each to the FTP directory. Once you've gained some experience with Unix commands and programs like these, you'll find it fairly easy to adapt them to your own tasks . For example, here's a more versatile command that searches a directory called Projects for all TIFF files modified after 6:00 that morning, converts them to thumbnail- sized jpegs, and then plops them into the images directory of your FTP-accessible Web server: find ~/Projects -type f -iname *tif - newermt 6:00 -print0 xargs -0 sips -Z 128 -s format jpeg out . ; ftp -u ftp:// carlos:birdie@ftp.coast-photo.com/ht- docs/images *
Of course, not everyone's going to want to type out that entire command line every time. Fortunately, you don't have to; having read the box on Section 17.1.6, you know that you can just save the whole thing as a .command file ”and run it thereafter just by double-clicking. Trouble is, a new Terminal window comes with the working directory set to your Home folder ”and you don't want your .command file to upload your entire life! Ideally, then, you should build a "change directory" command into the .command file. Change Terminal's attention to, say, a new, empty directory, which collects the processed files. Next, you should include a final move command that empties that directory when the rest of the command is done. That way, you'll again have an empty working directory for the next time you run the following command: cd ~/Stage ; find ~/Projects -type f -iname *tif -newermt 6:00 -print0 xargs -0 sips -Z 128 -s format jpeg out . ; ftp -u ftp://carlos:birdie@ ftp.coast-photo.com/htdocs/images * ; mv * ~/Backup/
With just a few more keystrokes, you could modify that command to collect some files, lock them, and place copies of each in every account holder's Home directory, as well as several different servers at the same time. What's more, it emails you a report when it's done. Using launchd , you could even configure this routine to trigger itself automatically every day at 11:00 p.m. Considering the hundreds of Unix programs included with Mac OS X and the thousands of others available on the Internet, the possibilities are limitless. For some guidance in picking up your Unix career from here, see Appendix E. |
< Day Day Up > |