farblog

by Malcolm Rowe

Stupid UNIX (shell) tricks

I was looking at the (now ancient, by internet standards) history meme a while ago. Here’s my results, at work:

100 (build)
87 p4
47 grep
41 cd
33 vim
20 ls
18 kill
16 (starting up a webserver)
15 rm
14 less

and at home:

88 ls
61 cd
35 less
31 curl
18 rm
17 svn
16 ./configure
12 su
12 make
12 cat

One thing I noticed when browsing through my shell history (yeah, yeah, I’m that sad) is that I make use of a load of little tricks that I didn’t know about only a year ago (when I wasn’t using the shell on such a regular basis). Here’s a couple:

Globs are great. In particular, brace expansion rocks:

diff -u some/long/tab/completed/path/file.{old,new}
cp another/long/path/to/file{,.bak}
ls src/java{,tests}/com/example/something

Piping is fun too. I run foo | less all the time, but I also do foo | cat occasionally. This is useful because it prevents foo from seeing stdout as a terminal, which makes some programs behave differently; for example, ps -f will trim command lines to the terminal width, while ps -f | cat will show the whole command.

The shell is a great calculator. Well, within limits: it’s great as long as everything you want to do will fit into a 64-bit signed int (so no floating point, and I can’t try out my favourite equation either). But it’s easy to use:

echo $((1 + 1))
echo $((0x41))
echo $((1 << 48))

Finally, here’s one I only started using recently: you probably know that you can use cd - to return from the directory you came from, but did you know that ~- expands to that old directory? I’ve using it when flicking between directories to do things like less ~-/somefile.