5 handy UNIX commands for Terminal.app lovers

Terminal Given Mac’s slick UI, chances are you’ll never bother with what is under the hood. Still, sometimes it helps to know a little bit of Terminal-foo. If you studied UNIX at school or college, you’ll feel right at home. Mac OS after all is UNIX under the hood and you can even find remenants of the green/ember terminal days like banner, talk, nl and yes, yes (last updated in Jan 93!). In fact, I recently ran through my copy of an old System V manual and was surprised to find most of the commands largely intact. Of course, a majority of them have changed drastically in scope and often sport new functionality/switches (no where is it more evident than the humble ls).

Here are 5 Terminal commands that might be useful. I assume you already know how to start Terminal, and get around (i.e you know ls, mv, cd, rm, mkdir, clear):

lsof – list open files. Run lsof, and it’ll list open files on your system; including names of the applications that are using them.

Give it the +D switch and a path as an argument, and it’ll show you all the files that are open in that directory. Very handy when an external hard-disk won’t eject, and the error message will not tell you what is holding it up:

Error Ejecting

$ lsof +D /Volumes/Movies
COMMAND    PID    USER   FD   TYPE DEVICE   SIZE/OFF    NODE NAME
bash       474 deepakg  cwd    DIR   14,5       1020       2 /Volumes/Movies/
diskimage  709 deepakg    3r   REG   14,5   14318026 1986830 /Volumes/Movies/Pan's Labrynth/Pans_Labyrinth_D1/Mac_DVD_ROM.dmg
...
...
...

You can even use lsof -i to see open IP connections on your machine.

$ lsof -i
COMMAND    PID    USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
SystemUIS  120 deepakg   10u  IPv4 0x5523530      0t0  UDP *:*
Finder     122 deepakg   14u  IPv4 0x61c1e64      0t0  TCP localhost:49152->localhost:26164 (ESTABLISHED)
Dropbox    128 deepakg    8u  IPv4 0x580966c      0t0  TCP localhost:26164 (LISTEN)
Dropbox    128 deepakg    9u  IPv4 0x61c1a68      0t0  TCP localhost:26164->localhost:49152 (ESTABLISHED)
Dropbox    128 deepakg   12u  IPv4 0x61c166c      0t0  TCP 192.168.1.3:49153->nitro-bro.getdropbox.com:https (CLOSE_WAIT)
Dropbox    128 deepakg   13u  IPv4 0x61c1270      0t0  TCP 192.168.1.3:49154->nitro-bro.getdropbox.com:https (CLOSE_WAIT)
Dropbox    128 deepakg   16u  IPv4 0x7cd4a68      0t0  TCP 192.168.1.3:50844->getdropbox.com:http (ESTABLISHED)
Safari     159 deepakg   25u  IPv4 0x7c8a66c      0t0  TCP 192.168.1.3:52510->apache2-bongo.nitti.dreamhost.com:http (CLOSE_WAIT)

Intrigued? man lsof will get you more info.

du – disk usage. Running du will show you “blocks” in use by each file an directory in your current directory. Run du -h to get a “human-readable” value – i.e. the size of the directory in kilobytes or megabytes. If you don’t have any sub-directories in a directory, running du -h will just show the size of the files in the directory. To list the individual files and their sizes, just use du -ha

$ du -ha
8.0K	./.DS_Store
4.0K	./DotEmacs
348K	./Icon
 16K	./JobSchema.txt
 19M	./SetFour.zip
 24M	./SetOne.zip
 22M	./SetThree.zip
 22M	./SetTwo.zip
4.0K	./Top Secret.txt
 87M	.

file – determine file type. The other day I got an attachment on GMail without any file extension. I could drop the file into textedit, look its contents and try and figure out what it was or I could just use the Terminal command file. Run file with the name of a file and it’ll tell you what it is. Often it’ll tell you a little bit more than that, e.g. running it on – what turned out to be a png file – gives me the size and color info as well:

$ file Randomfile
Randomfile: PNG image data, 723 x 832, 8-bit/color RGB, non-interlaced

grep – prints the lines in a file that match a given pattern. I use it often as a poor man’s “find in files”. Some useful switches include -H: to print the name of the file in which a match is found, -R: to recurse subdirectories and -n to include line numbers where a match is found. e.g., to search for all files containing the word DocumentRoot in /etc, I would use:

$ grep -HRn "DocumentRoot" /etc/*
/etc/apache2/extra/httpd-ssl.conf:77:DocumentRoot "/Library/WebServer/Documents"
/etc/apache2/extra/httpd-vhosts.conf:29:    DocumentRoot "/www/docs/dummy-host.example.com"
/etc/apache2/extra/httpd-vhosts.conf:38:    DocumentRoot "/www/docs/dummy-host2.example.com"
/etc/apache2/httpd.conf:159:# DocumentRoot: The directory out of which you will serve your
/etc/apache2/httpd.conf:163:DocumentRoot "/Library/WebServer/Documents"
/etc/apache2/httpd.conf:188:# This should be changed to whatever you set DocumentRoot to.
/etc/apache2/httpd.conf:307:    # access content that does not live under the DocumentRoot.
/etc/apache2/original/extra/httpd-ssl.conf:77:DocumentRoot "/Library/WebServer/Documents"
...
...
...

find – finds a file based on several parameters like its name, size etc. While on its own it might not seem useful, it can come in very handy when used with another command. e.g. to add all files in my ~/Pictures folder that look like IMG_3 and are bigger than 30 MB to a zip file, I can do something like this:

$ zip archive.zip `find ~/Pictures/ -name "IMG_3*" -size +30M -print`
adding: Users/deepakg/Pictures/coorg/IMG_3951.psd (deflated 3%)
adding: Users/deepakg/Pictures/London/IMG_3358-02.tif (deflated 3%)
adding: Users/deepakg/Pictures/Converted/IMG_3368-01.tif (deflated 3%)

Do you have any favorite Terminal commands? Let us know in the comments!

  • dc
    you guys rock
blog comments powered by Disqus