The Beginner’s Guide to Command Line Magic: Part 2
In the first part of this series, I showed you the basics of getting started on the Mac OS X command line. Once you’ve got your feet wet with the tips there, continue with Part 2 below.
Fink
For those unfamiliar with Fink, it is a project that unleashes the beast that is BSD from under the gorgeous shell of OS X. It is an open source project that modifies Unix software so that it compiles and runs on Mac OS X and provides a tool for easy installation. Read my review and brief tutorial of Fink to get started with it. This allows you to get your hands on a lot of the cool tools the Linux/Unix community brags about, while remaining firmly grounded in your Mac OS world.
A lot of tools available through Fink are not command line, but some are. And many programs in the Unix world have both GUI and command line versions, like Fink itself. The GUI version makes it easy to do certain things quickly, but the GUI unleashes these programs’ true power and allows tasks to be scripted, combined with other programs, and so forth.
Of course, it wouldn’t behoove any good student of this series of articles to use the pretty GUI for Fink. You can use Fink on the command line as well—just type fink install (packagename) on the command line to install anything you want. And man fink will tell you everything else you need to know about all the options and arguments (you do remember those, don’t you?) you can use with Fink to tap its power.
Back to Our Regularly Scheduled Programming
Now that you have Fink installed, you can play with it at your convenience. Installing and uninstalling packages is easy, and will rarely affect the rest of your system since everything Fink does is in the /sw/ directory, far from the rest of OS X. Here are a few more interesting things you can do off the command line:
List Open Files: lsof
Simple Usage: lsof
This command allows you to list all the “files” on your system that are currently open. Of course, I use the term “file” broadly since it encompasses every type of file (including system files), directories, network sockets, and much more. This is a lot of information and will scroll quickly up your screen unless you remembered to use more from our last lesson (lsof | more).
To make this information more useful, take a look at the man page for this command. An easy way to tap its power is to pipe it to the grep command. Remember grep? It allows you to list only lines that match on a particular search string. So if you want to know which files iTunes currently has opened you would pipe it as follows: lsof | grep iTunes.
One interesting Mac use of lsof that I employ once in a while is to see which song people on my network are listening to off my computer. Since iTunes allows you to share music on your home or office network automatically (using Bonjour), lsof allows me to easily find which song they are accessing right now with a quick lsof | grep mp3 which works because all my music is in MP3 format. If yours is in .m4a, grep accordingly.
Compare Text Files: diff
Simple Usage: diff (filename1) (filename 2)
The diff command is simple and immensely powerful. If you feed it two file names (both text files) as arguments, it will return all the differences between those files and which lines they are on. This is extremely useful in programming, but I am sure you can find many other uses for it. If you send a friend a text file and she sends it back to you edited, you can run diff on the two files to find precisely what changes she made.
Disk Usage: du
Simply typing du on the command line will give you the Disk Usage of every directory that is inside your present working directory, including sub-directories (remember, use cd to change your present working directory and pwd to find out what it is). This allows you to quickly find the worst offenders in terms of disk usage, if you need to detect where you are wasting disk space.
Using du -a allows you to list the disk usage of every single file in the directory (again, including every sub-directory). This is useful, for example, if you want to find the size of every word document anywhere under your home directory. Simply go to your home directory (type: cd) and then type du -a | grep “.doc” (this can take a while if you have a lot of files). Nifty, right?
Not satisfied with that much? Well, play around with Fink, and here are a few commands to try for extra credit: df, cat, and wcwc. Stay tuned for Part 3 soon.
Comments