I’m on a Mac and I love working from the command line (i.e. from within the Terminal or iTerm). Here is a random collection of some of my favorite “hacks” (time-saving tips and tricks), which will also work not just on Mac OS X, but in Linux/Unix distributions such as Ubuntu and with whatever shell you prefer (bash, csh etc.)…
(Please note: I’ve assumed you have Perl installed and in your PATH. If you don’t know what I’m talking about, these tricks are not for you, and you’ll probably do some damage. You have been warned. )
Search and replace within a file – without launching a word processor!
Hypothetical example, replaces all occurrences of “chicken” with “fish” in a text file called somefile.txt and saving a back-up (just in case!) to somefile.txt.bak:
perl -pi.bak -e 's|chicken|fish|g' somefile.txt
Real-world example, using regular expressions. Replaces all dates in a dd/mm/yy (British date) format in a Quicken QIF file (that I downloaded via online banking from a bank in New Zealand) with American date format mm/dd/yyyy for use in the American version of Quicken:
perl -pi.bak -e 's|(\d\d)/(\d\d)/07$|\2/\1/2007|g' download.qif
Another advanced example, that takes all the .html files in the current directory and strips out anything that was commented out using comment tags, i.e. <!– –>…
perl -pi.bak - 0777 - e 's|<\!--.*?-->||gs' *.html
Rename a bunch of files in a directory – in batch!
This example renames all files in the current directory ending with .jpeg so they end with .jpg instead:
for i in *.jpeg; do; mv $i `echo $i|sed 's/jpeg/jpg/'`; done
Backticks (`) are amazingly powerful. With them you can execute commands and have the output of that command get used in another command. Yep, nested! Awesome!
Batch resize images
This example creates thumbnail images of all the .jpeg files in the current directory, each being no bigger than 400 pixels in either dimension, and the height-width aspect ratio is kept intact (i.e. the image is not stretched). Each thumbnail filename is prepending with the word “small”, followed by the original file’s filename (e.g. from lolcat.jpeg to smalllolcat.jpeg):
(Note that this hack requires you to have the ImageMagick package installed, which includes the very handy “convert” command. To check whether ImageMagick is installed, type “which convert”, then if nothing turns up, try “locate mogrify” (I didn’t suggest “locate convert” because that would probably turn up a lot of other apps and files besides ImageMagick’s convert). The easiest way to install ImageMagick on the Mac is to download a precompiled binary pkg file.)
for i in *.jpeg; do; convert -geometry 400x400 $i small$i; done
(Tip: This is especially handy for when you want to upload a bunch of images to Flickr but don’t want to waste bandwidth and time uploading the high-resolution originals.)
Batch convert the image format on a bunch of files
This one takes all the .jpg files in the current directory and creates .png files out of them. You could alternatively convert pngs to jpegs, gifs to jpegs, jpegs to gifs, gifs to pngs, etc. etc. Each filename has the same base as its counterpart (e.g. from lolcat.jpg to lolcat.png).
(Note that this also requires ImageMagick.)
for i in *.jpg; do; convert $i `echo $i|sed 's/jpg/png/'`; done
Examine the type of redirect (301 or 302) on a URL and where it leads – great for following a chain of redirects!
Sure, you could do this with the livehttpheaders extension but I don’t use Firefox, I use Safari ‘cuz it’s so blazingly fast on the Mac compared to Firefox or even Camino.
(If you don’t know why you should care about which type of redirect you’re using or whether it’s a long chain of redirects, you’d better read my Search Engine Land article: Redirects: Good, Bad & Conditional.)
Here I’m looking at the hypothetical example URL of www.example.com…
(Note that you don’t need to use a properly formed URL beginning with http://.)
lwp-request -Sd www.example.com
Don’t forget to put the URL inside of quotes if the URL is a dynamic one with question marks and ampersands (or other special characters). Like so:
lwp-request -Sd "http://example.com/search?q=cheese&num=20"
More likely than not, you don’t have lwp-request installed already. If that’s the case, it’s easy to install LWP, it’s just a CPAN library. Install it like so:
sudo cpan install LWP
Enter “no” when prompted whether you are ready for manual configuration.
Then hit the Return key a bunch of times
If it can’t find your “make” program, you can configure it like so, using whatever your path is to make (in the example below I’ve assumed /usr/bin):
perl -MCPAN -e shell
o conf make /usr/bin/make
Back up your MySQL database to a SQL file – especially before running a WordPress upgrade!
I’ve used the hypothetical database name of “databasename” in the example below:
mysqldump -udba -p databasename > databasename.sql
Consider adding this command to your crontab if you want to have regularly scheduled database backups (
crontab -e for that).
Back up your folders using rsync
Sure, if you’re a programmer you probably live your life within a version control system like Subversion (svn). And so for you it will be trivial to put your important documents in a Subversion repository and under version control. But I don’t live and breath svn. I know enough to be dangerous – and I intend to keep it that way! (I practice Tim Ferriss’ philosophy of “selective ignorance“.)
If you’re lucky enough to own a Mac, just buy a Time Capsule and be done with it. But for the rest of you, you can use rsync to remotely sync (back up) your computer.
Here’s an example of backing up your Documents directory onto a remote server:
(Note: This assumes you’ve set your domain name using “domainname” or you have “Search Domains” in your Network settings set to your local domain name. Otherwise use the full hostname with the domain name in the following command, e.g. eeyore.netconcepts.com instead of just eeyore.)
rsync --force --ignore-errors --delete-excluded --delete -av -z -e ssh ~/Documents root@eeyore:/home/stephan
Here’s an example of how you can back up your mail folder from the mail server onto a local backup drive:
rsync --force --ignore-errors --delete-excluded --delete -av -z -e ssh root@eeyore:/home/stephan/mail /Volumes/BackupDrive
View the code in a binary file
Strings shows only the text that can be extracted from the binary file. I use it when I don’t want to launch Microsoft Word (a memory hog that takes 30 seconds to start up on my machine) but I want to quickly view a Word document’s contents, like so:
If I need to examine the data within a binary file, byte by byte, I use od (stands for octal dump) rather than strings. Like so:
od -c something.exe
od is also handy for examining the ASCII codes of strange characters in a HTML page, like so:
lwp-request http://shop.bellacor.com/results-13/70/1.htm | od -c
History repeats itself
I use the up arrow key all the time to pull up previous commands so I can revise them and reissue them. Once the chosen command is displayed, I can use backspace, delete, etc. to change it. An even better way to do it is to use the ^ operator if you simply want to swap out one word for another, like if you made a typo.
For example, if the previous command entered was a typo:
covert -geometry 200×200 big.png small.png
Then you could correct that simply by typing:
I love referring back to my history and re-using previous commands. Sometimes I want to scan all my history, for that just type:
Another huge timesaver is the exclamation point. But do yourself a favor and refer to it as the “bang” operator. So if you were to spell out out !con to your geek friend over Skype, you would say: “bang c-o-n”. That’ll make you sound like you’re in the inner sanctum of all geeks.
Here’s how it works… Bang followed by a number executes the command at that specified line number in your history output. For example:
Bang followed by letters executes the command in your history that begin with the specified letters. So if I wanted to rerun the last ping command I ran a while ago, I’d just type:
Or if I’m extra lazy I might type:
I wouldn’t type !p because I could accidentally issue an unintended command if I had forgotten I had used more recently and that also happened to start with a p.
What about QuickSilver, you ask?
If you’re a Mac power user, you’re probably using QuickSilver and getting big productivity gains from it. Well good for you! I myself don’t use it that much, primarily just for launching applications. But I plan to use it a lot more. I just have to get myself into that habit. If you’re a Mac user and want to learn more about QuickSilver, the best introduction to it (imho) is this video of Merlin Man demonstrating it on MacBreak. I don’t see the bash shell (Terminal) and QuickSilver as mutually exclusive. Really they go hand-in-hand.