BOLTS : Click to return to MacEdition homepage
 

(a few) Productivity Tips For The Unix User

March 10, 2003

Feedback Farm

Have something to say about this article? Let us know below and your post might be the Post of the Month! Please read our Official Rules and Sponsor List.

Forums

Want to dig even deeper? Post to the new MacEdition Forums!

OK, so you’ve made the switch over to Mac OS X from another Unix flavor (Linux, Solaris, whatever). What are some of the goodies that OS X brings to the table?

The Finder and the Command Line – not quite oil and water

As you can probably tell by now, I’m a big fan of the Unix command line. GUI niceness has its place, but there are times when the command line is the fastest way to get some kinds of work done. If you’re coming from a purely CDE, KDE, or Gnome environment, it’s worth your time to investigate the command line. The OSXFAQ folks have a great Unix reference, and tip of the day mailing list on the Unix command line and shell tricks. It’s worth checking out and subscribing.

For instance, the locate command will consult a database on file names stored on your system and return the paths for all the files that match. This location of files is very fast, much faster than having a program walk the disk looking for the file. locate can spew out a lot of information, so judicious piping and greping can come in handy. For instance, you can find all of the .app bundles on your system by performing

% locate .app | grep .app$

While many Unix folks feel there is no substitute for the command line, there are some operations that are much easier in the Finder. Want to open groups of files that don’t have much in common in their names? Want to easily deal with spaces or weird characters in the filenames (no, escaping spaces is not considered "easy" in this context)? How about opening files in programs other than their default application (again, opening everything in Emacs doesn’t count)? The Finder (either out of the box or with the addition of simple utilities) makes these sorts of things much easier (even when you’re at the command line).

One command I use all the time is open. That tells the Finder to open whatever file or directory you indicate. If you give a directory to open, like with open . or open $HOME, the Finder will open a new window into that directory. That way you can cd to your heart’s content, realize “I want to drag some files here over to a CD to burn”, so you just do an open . and mouse around.

Going the other direction, where you’re looking at a Finder window and want to open a terminal in that directory, you can use Marc Liyanage’s Open Terminal Here script. Drag this up into your Finder toolbar, and then click it when you want to open a terminal window at that location.

There are two Finder features I use frequently. Type-ahead selection and contextual-menu open. With type-ahead selection, you can easily select a needle in a filesystem-window by typing the first few characters of the name of the file (all bets are off if you’re in your browser’s cache directory). Type part of the name and there you are – the file is selected. The contextual menu open is another nifty feature that helps out when you want to do something besides the default (for example, open a PDF file in Acrobat and not Preview). For the most part Preview is fine for looking at PDFs, but every now and then I download a PDF I want to read in Acrobat Reader; so I open my downloads folder, type the name of the file, then control-click and drag it over Acrobat Reader. Very quick and painless.

If you still insist on using the command line, open can accomplish the same thing. For a book I’m writing, I want to use Acrobat to read the PDF that is generated by the SGML tools. I use the command line to generate the PDF from my SGML source, and then do a open -a "Acrobat Reader 5.0" ~/Projects/core-osx/Book.pdf. That’s a mouthful, but a shell alias turns that into a single short command. You can also give open the -e flag so that it will use TextEdit – very handy if you want to poke around a text file but not have it opened by some other application.

Disk Mania

HFS+, the Mac OS X file system, differs from traditional Unix file systems in being case-preserving, but not case-sensitive. Traditional Unix file systems let you have files called Makefile, MAKEFILE, and makefile in the same directory, and they’re all distinct files. In HFS+, there could only be one file called makefile regardless of capitalization. For the most part, this is no big deal. If there is stuff you need to use that requires this case sensitivity (like some Perl modules), you can partition your disk and make a UFS (Unix file system) volume. Important warning: do not reformat your entire disk with UFS and then install Mac OS X - it will eventually fail in weird and wonderful ways because some programs and utilities assume the HFS+ file name semantics.

Repartitioning your disk to add a UFS volume can be a major pain in the butt, especially if you don’t size your partitions right. You might find you didn’t allow enough space on your UFS partition, or you discover you don’t use it enough to justify the space it is consuming. The slick solution to this is to use the Disk Copy program (located in the Utilities folder in Applications) to create disk images. Disk images are just files on your disk that the OS treats as if they were physical devices. A lot of applications, both commercial and free/shareware these days ship on disk images (these have the .dmg extension).

The Mac OS X disk images are actually really neat. You can create a UFS disk image and mount that when you need UFS stuff. If you need to, you can do a newfs on a disk image to customize parameters like inode density or block size. You can also create encrypted disk images to store private stuff on. You have to supply a password before it can be mounted and accessed, and since it’s encrypted, someone who has the disk image file can’t read the contents.

AppleScript

Another unique Mac OS X technology is AppleScript, a scripting architecture where you can write little scripts to make applications jump through hoops. AppleScripts are different from traditional Unix scripts, which hook together a bunch of short-lived programs to do some operation, in that AppleScript lets you manipulate already running programs into massaging data or eliciting some behavior you’re interested in. The “Open in Terminal Window” utility mentioned earlier is an AppleScript that interacts with the Finder (to get the directory the user is looking at) and Terminal.app (to tell it what directory to use when opening a new window). The iApps (iPhoto, iMovie, etc.) are scriptable so you can hook them together in custom ways. For instance, there are scripts out there that hook iCal up with iTunes, so that iCal actions cause music to play at a given point in time.

If you install the development tools (which is a good idea because you get the CVS tools), you get Apple’s AppleScript Studio, which is a programming environment that lets you create programs with a native Mac OS X interface, but are implemented using AppleScript. These are handy for one-off applications, or if you want to write a program but you’re not ready to dive into Objective-C and Cocoa yet.

A good AppleScript resource is Bill Cheeseman’s AppleScript Source Book website, which has a ton of stuff. Apple also has information at http://www.apple.com/applescript/, as well as documentation for many of the iApps AppleScript dictionaries at http://www.apple.com/applescript/apps/.

Mark Dalrymple (markd@borkware.com) has been wrangling Mac and Unix systems for entirely too many years. In addition to random consulting and custom app development at Borkware, he also teaches the Core Mac OS X and Unix Programming class for the Big Nerd Ranch.

E-mail this story to a friend

Talkback on this story!