That Mac you're viewing this web page on using a pretty graphical interface? That's a Unix-based system which can run the powerful and age old command line utilities of the most advanced Unix beard. If you've never launched the Terminal, you're missing out on a plethora of Unix tools that offer more control and options than any dialog box possibly could. We've covered some basic Unix command line techniques in the past, but today we've got a few more for folks who want to start their Unix ninja training from the comfort of their own Mac.
Replace Terminal with iTerm
The first stop in your foray into Mac OS X command line goodness is getting yourself a proper terminal. While all the commands we'll discuss will work just fine in the build-in Terminal.app (located in /Applications/Utilities), the free, open source iTerm is a definite improvement over vanilla Terminal. Mostly because it supports tabbed sessions, which lets you run several tasks in various tabs without taking up screen real estate. Download iTerm here (free). iTerm tab hint: once you've got more than one tab open, use the Ctrl+Right/Left arrow to move between them.
We've already posted a series of beginner's guides to the command line for Windows users running the Unix emulator Cygwin. This is the beauty and advantage of being comfortable at the Unix command line: learn commands once and they'll work most anywhere—on Mac, in Windows/Cygwin, and in most flavors of any Unix-based system. So instead of traversing ground we've already covered, hit up our past tutorials instead (and ignore all the Cygwin-specific stuff):
- Introduction to Cygwin—Print the working directory with
pwd, create new files with
touch my-new-file.txt, and list all the files in a directory with
- More useful commands—File listing command options (like
ls -lh), aliasing common commands to save typing (like
alias 'ls'='ls -lh'), append text to files with >>, see the contents of files with
cat, search file contents with
grep, using your command
historyand RTFM'ing with
- Scripts, packages and more—Logging onto other computers remotely from the command line using
sshand scripting collections of commands for easy execution.
While almost the entire recipe box of established Unix commands work on OS X, there are also a few Mac-specific command line tools that hook into Spotlight, access your iPod and other external disks, launch Mac graphical applications and install more open source applications. Let's take a look.
Launch Applications and Documents
open command can open up a certain file in its associated application (like
open lifehacker.doc will launch Word with lifehacker.doc open) or it can launch a specific application. To open a document, you've got to be in the directory where that document lives; to launch an application, use the
-a option to tell Mac OS X to look in the Applications folder. For example,
open -a ichat will launch iChat no matter what directory you're in. Using previously-mentioned aliases, you could map the command ichat='open -a ichat' in your
.bash_profile to save yourself some keystrokes, too.
Access Files on Your iPod (and Other External Volumes)
Now that you know how to navigate folders and subfolders and file listings, you may want to use your new command line knowledge to check out external disks connected to your Mac, like your iPod. Mac OS X lists all external drives in the /Volumes/ directory. So to get there, type
cd /Volumes/ and hit Enter. Then a quick
ls will list the available drives. Here I've got an iPod (with disk use enabled in iTunes) called "Terra's iPod" connected to the Mac. Move into it using
cd Terra's iPod (you can use the Tab key after T to autocomplete the volume name, which is a little screwy because of the apostrophe) and then
ls the directories there. Your iPod's music is stored in the /iPod_Control/Music directory, so you can
cd there to see how all your music shows up:
Sadly your music is stored in folders with non-obvious names, like F00-F49.
cd into any one of those directories and you'll see similarly-named music files. To copy those files back to your Mac's internal hard drive, a simple
cp command would get the job done.
Access, Search and List Spotlight File Metadata
Mac OS X's built-in file search system Spotlight indexes a lot more than just file names and contents. It builds an index of metadata like file type, author, times and dates and other information, like artist and album for properly tagged music files and camera model information for digital photos. Using the
mdls command, you can list Spotlight's metadata for a file and using
mdfind, search for files that only match certain criteria. For example, to see the metadata for a Word document, I'd do an
mdls filename.doc, as shown:
To see other documents authored by Marcia, I'd use the
mdsfind command with the filtering parameter
"kMDItemAuthors == 'Marcia Ellett'":
Install Open Source Software with Fink
The Fink Project ports open source Unix software to run on Mac OS X's Darwin and makes that software available for download to your Mac in a simple command. Once you download and install Fink, you can use the
sudo apt-get install emacs, for instance, to install the classic Emacs editor. Or you can browse the list of available packages in Fink using the free Fink Commander graphical interface, which looks like this:
More Fun Stuff
Other Unix fun to be had on your Mac (or any *nix system) includes:
- Encrypt your web browsing session with an SSH tunnel
- Automatically download entire web sites, new music and more by mastering wget
- Run a personal, home SSH server
- Customize your command prompt (like my pictured prompt, "Your wish is my command")
For more on Unix for Mac users, check out Dave Taylor's excellent book, Learning Unix for Mac OS X Tiger, which inspired and informed this article.
How do you use the Unix goodness baked into your Mac? Let us know in the comments.