If you are just starting to use the UNIX/Linux command line, go read UNIX/Linux tutorial for beginners or even Learn UNIX in 10 Minutes. Natbat's Terminal Reference has prompted me into trying to shove all the various UNIX and OS X brain-worms onto wikipaper.
General principles and why
- If it's a repetitive or non-interactive task, do it at the shell rather than using yet another GUI tool for it. It's usually quicker, less error-prone and when it does fail, you'll get a more useful (though less pretty) error message. Examples:
- File conversions (images, audio etc.). I tend to convert lots of audio and image files into different formats. On the Mac, when I last looked, there was only one tool I found that handled FLAC-to-MP3 conversion. It featured an icon of a gorilla wearing headphones or something. But worse than that, it couldn't do a folder of FLAC files, but only do one at a time. It was completely painful. But if you install flac and lame at the command line, you can do `flac -cd {file}.flac | lame -h - {file}.mp3`.
- Large files. Sometimes I want to slice a PDF. Not often mind, but sometimes. Ghostscript is my go-to tool for such operations. I've tried Acrobat and other high-end tools. Generally, if you've got a really large file (I've got some PDFs that are hundreds of megabytes), the GUI tool has to load it into memory before you can play with it. If you use `gs`, you can specify what you want and it'll happily churn away in the background, rather than eating up system resources drawing lots of fancy GUI stuff. You can then use the resources you've saved to, oh, play some emulator games.
- Scriptability is another reason to use CLI tools. While AppleScript, despite it's abortion of a syntax, lets you automate and script some aspects of Mac GUI applications, it's often much more efficient just to go straight to the real thing.
Quick hit wins
- Obviously, man pages are vital. Read `man man` and `man apropos`. If you use a particular language, look at that language's documentation tool and understand it: (for Ruby, learn ri and rdoc, for Java, learn javadoc). I tend to work on the principle that if I'm looking something up for the third or fourth time in a man page that week, I scribble it down on to a piece of paper and carry it around with me until it's firmly in my brain. But there's more: look at cheat, especially if you use Ruby a lot. You can run `cheat tar`, say, and get back a cheat-sheet that'll tell you how to use tar. They are like user-generated man pages, but they contain much more usage examples and much less exhaustive documentation of every single flag and mode.
- If you use OS X, learn how to use the `osascript` command. osascript is basically the command-line interface to the Open Scripting Architecture, the underlying component of what makes AppleScript work. You can pass AppleScript strings to osascript and have them execute. The nice thing is that you call osascript from your scripting language, and then do all the actual logic in Python or Perl or Ruby or whatever, where you get proper variable assignment ("foo = 5" rather than "set foo to be the number 5, please dearie"), regexes, logic code that's not totally painful and the ability to interface with web services, other non-Cocoa apps. Look at MacPython, MacRuby and the various other Objective-C and OSA bridges.
Know thy finding commands
- `find`, `locate` (or `slocate`) and `grep` do different things. Read the manual pages for each and know when to use them.
- If you use a Mac, look at mdfind - it gives you a CLI interface to the Spotlight.
- On Linux, similar command line tools exist to use the GNOME Beagle tool.
Terminal.app fixes, and why you should learn screen
- Remap your home and end keys to do the same as Ctrl-a and Ctrl-e
- If you find yourself opening up more than one or two tabs in your terminal emulator, learn how to use GNU Screen. It's, err, tabbed browsing for your terminal. ;) Actually, it's much more. It lets you run as many shells as you like within one shell and then jump between them. But the really awesome bit is that it lets you attach and detach those shells.
- Here's an example of why that's useful. I have a Linux box under my desk that runs a whole bunch of things for me: I store files on it, read and send e-mail, read netnews (aka. USENET), have IRC going, and have all my dev tools on there. I had some data I was processing recently: about 2.7Gb of fairly gnarly HTML. I had written some code on my laptop that I wanted to run against it. Once the code was written, I compiled it (Java) and SCPed the resulting classfile onto the machine. I logged in over SSH using my 3G connection, reattached my existing screen session, made a few new screen shells: in one I ran the code against the dataset. While it was executing, my connection went down. I reconnected again, logged back in, `screen -d -r` and my shells were as I left them.
- Another useful screen use is for downloads. If I'm out and about and want to download something large, I can login to my Linux box at home, make a screen shell, run curl or wget, then detach it. Yes, I could do it with `wget [url] & `, but who needs it? I can just log into my existing shell and let it run on. And, well, the screen shells are not subject to the unfortunate mishaps that can happen to their hosts. If Terminal.app crashes, the screen shell contained therein just lives on quite happily. Same for if I log out of my OS X session or X Windows session. Obviously, screen shells won't carry on after a machine has been rebooted (someone please send a feature request).
- There's a whole bunch of things you can do with screen that I don't: split screening, sharing one's screen, running the same shell on two different computers simultaneously (duplexing or whatever).
Audio
- If you listen to MP3s, you may find that mpg321 or another MP3 decoder is a useful addition to one's screen shell. Before you say WTF:
- <pre>USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
tom 52832 2.5 0.2 606708 3496 s001 S+ 10:14pm 0:00.74 mpg321 ...tom 137 0.0 2.8 1083980 58992 ?? S Tue11pm 154:06.93 [...]iTunes
tom 132 0.0 0.1 879128 1692 ?? S Tue11pm 0:01.04 [...]iTunesHelper[...]