UNIX Terminal Command Line Tips for Mac OS X Geeks
I've spent decades working with UNIX and, later, Linux boxes. In 2004 I started looking at the one UNIX box I had not really played with: Mac OS X. So, I bought a copy of Mac OS X Panther for Unix Geeks (by Brain Jepson and Ernest E. Rothman), read the Mac articles O'Reilly's Mac DevCenter site and learned that underneath Mac's graphical goodness lay a full UNIX workstation accessible through a familar (to me) command line text window (Terminal). This was exactly what I wanted: A workstation with a great graphical interface, rich third party software library, and the full power of UNIX from a command line interface. I then waited for Apple to release its then rumored lower cost headless desktop: The Mac mini. After buying my first Mac in early 2005 (Mac Mini Eye for the Linux-Windows Guy), I've found I have a Terminal window up nearly 100% of the time when using a Mac. If you rarely or never use the command line from Terminal, you might want to browse through the list below to see if something there might give you a reason to try the command line sometime.
Here's a set of ten Terminal shell tips (11 if you count tip 0 :-) for using Terminal, the Bash shell, and various UNIX utilities to get the most from a plain unadorned text terminal window.
- OS X's Terminal can be used with any available shell. The default, however, is the Bash shell (Bourne Again Shell - a play on words as it is an improvement on the original Bourne Shell). The Bash shell is a rich command line tool with filename completion, command history, a complete programming language, and more. Its power is augmented by the mature, tried, and true set of UNIX applications on the Mac in /usr/bin, /usr/sbin, /sbin, /usr/local/bin, and /usr/X11/bin. These mostly single function applications can be used in combination to perform all kinds of functions. There are 743 UNIX utilities in /usr/bin alone (found using the commands ls /usr/bin | wc)
- df -h: The df command is used to display free disk space. The -h option tells it to report this information in human readable format. This means it displays total size, used, and available disk space in terms of gigabyte and megabytes instead of the number of 512K blocks.
- du -s: du provides information about disk usage. This is really handy when you want information about how much space files in specific folders are using. In the example here, you can see that the du command can be given more than one folder to look at and reports each one separately. The oddly named .Trash file points to the OS X Trash can. Dotted files (file names preceded by a period) are generally invisible when looking in folders even from the Terminal command line. However, they can be worked on like any other folder. The -sh option shows how UNIX commands can be given multiple options. The dash tells du options are ahead. The s option tells it to report each folder (or file) separately. The h tells du to report in human understandable values (megabytes in this case).
- ls: ls lists files and folders. This seems simple enough and, like all Unix command line commands, its strength lies in its power of doing just one thing and doing one thing well. Here's a simple example. Let's say you wanted to find all files in a folder that has the string 'pl' in it. This is how you would find these files: ls -lh *pl*. The l option tells ls to list files with details and the h options (again) tells ls to provide human understandable file size information. The asterisks (*) on each side of the string 'pl' tells ls to find all files with that string regardless of what is on either side of it. There's a lot more to ls, of course. It can list files recursively. It can list files in folders other than your current shell folder location. It can list invisible dotted files (-a) and has many other options for display basic information about files and folders.
- find: The find command description is walk a file hierarchy. That doesn't seem like much at first glance. However, find is possbility the single most powerful single function UNIX command. It can, of course, look down a folder and all its subfolders to list certain files. For example, find . -name "*.html" -print will find all files ending in the string html. However, it can also do something with the file. For example, file . name "*.html" -print -exec grep -i podcast {} \; will find all the files ending in the string html and then use the grep command to search for the string podcast regardless of case in each file. You can also do something like find . -name "*foobar*" -print -exec rm -f {} \; which would remove (delete) every file with the string foobar somewhere in its name in every folder under and including the current folder you are in. Use the find command with great care.
- less: When working on the command line in a terminal window, you often generate a lot more text than the default 24 lines displayed in Terminal. You can resize the window. But, this only goes so far. less lets you look at long text files with simple keyboard navigation features. You can also search a text file or fire up a text editor while viewing a file.
- kill/killall: Every now and then an application gets in some weird state where it does something that drags your Mac to a near halt. Or, things may seem normal but some appliaction won't shut down properly. kill can terminate an application by its numeric process ID (PID). killall terminates an application by its process name. Use the ps (process status) to get either the PID or the process name. Note that case is significant. ps cx | grep -i safari searches for any process with the string safari ignorning case (-i). So, safari is different from Safari. The Safari browser process ID has an uppercase S. So, the command would be killall Safari. The PID (first number on the left here) can be used with kill like this: kill 2162. If a process is resistant to being terminated, use the -KILL to force it to end.
- mv: Moving files (not copying) from one folder to another can sometimes be simpler to do using the mv command from the command line. For example, if I have a bunch of JPEG files with filesnames ending in .jpg on my Desktop, I can move all of them to the current directory I'm in (on the command line) by typing mv ~/Desktop/*.jpg .. The tilde (~) is shorthand for my home folder (/Users/todd). The asterisk (*) is once again a wildcard pattern matcher. And, the period (.) is shorthand for the current directory I'm sitting in. I find this much faster than selecting files and moving the mouse from one window to another.
- ping: Networks are odd critters. They shouldn't be, but they are. Stuff just seems to happen for various reasons including flaky hardware. ping sends an echo request packet to a network device. If that device allows echoing it back (and many people turn off this characteristic for various reason), it can help you narrow down where a network problem might be. For example, if ping www.yahoo.com doesn't return a series of packets, try pinging your local gateway. If you are in your home, you probably need to ping the IP address of your home router. For example, ping 192.168.0.1. Press Control-C to stop the ping echo process.
- vi/emacs: I don't always need or want a full graphical text editor like TextWrangler or Komodo Edit. This is especially true if I just need to edit a line or two. Both the vi and GNU Emacs text editors are available from the Terminal command line and run inside the Terminal window. I tend to use vi because that is what I learned first many years ago. But, it is really a matter of personal preference.
- man: Finally, if you need more information about any of the UNIX commands mentioned here, use the built-in man (manual) program. Just type in the name of command after the word man and a space. For example, man vi or man find. There's also the apropos alias for man -k. But, that requires becoming root and building the index first. I'll save that for another blog entry.
Even the most GUI-oriented Mac user can probably make use of some Terminal command line features now and then. As I said, I almost always have a Terminal window open while using my Macs.
Categories
MacRead More Entries by Todd Ogasawara.







Nice commands.
Ashok1729
Andrew: You can set vi style editing in Bash by typing 'set -o vi'. You can revert back to the default Emacs style editing by typing 'set -o emacs'
Ya, me too switched to get the full unix guts... just that I forgot a few things. Maybe you know, how do you get vi editing features on the command line, like cw or skip words etc... used to have this on Linux but I wonder if bash shell offers it... or do I have to switch to tcsh.
great article.