2 minute read

Update (2/20/09): Check out Useful unix tricks – part 3 and Useful unix tricks – part 4

I covered a bunch of unix command line tricks in my previous blog post: Useful unix tricks. Here are some more.

Learn vi

Even if you prefer another text editor (such as emacs), it is worth your time to learn vi. vi is installed on every unix box, whereas emacs and others are not. Furthermore, many other unix tools use vi shortcuts (such as less), so learning the shortcuts will help out with other commands.

Use less instead of tail

I find that I often want to see the end of a file. The first command that jumps to mind is tail:

% tail /var/log/messages

Inevitably, I realize that I need more than the last 10 lines. So my next command prints more lines from the bottom:

% tail -n 100 /var/log/messages

However, an easier approach is to use less to display the whole file and then jump to the bottom (using Shift+g):

% less /var/log/messages

Now, I can scroll up and down at will.

mkdir -p creates nested directories

By default, mkdir will only create a top level folder. So the following command will fail:

% mkdir some/nested/folder
mkdir: cannot create directory `some/nested/folder': No such file or directory

However, if you pass the -p parameter, mkdir will create all of the nested directories.

% mkdir -p some/nested/folder

ps will show process trees with the f flag

The ps command shows a list of running processes. The f flag will show the processes in a tree, so it is evident which processes started other processes. For example, a normal ps output looks like:

% ps
  PID TTY          TIME CMD
 6071 pts/2    00:00:00 zsh
 6287 pts/2    00:00:00 ps

The f flag output looks like:

% ps f
 6071 pts/2    Ss     0:00 /bin/zsh
 6288 pts/2    R+     0:00  \_ ps f
 5946 pts/1    Ss     0:00 /bin/zsh
 6126 pts/1    Sl+    0:18  \_ ruby ./script/server

Now, it is clear that I have two terminals (with zsh shell) running. One of them ran the ps command, and the other is running ruby script/server.

Normally, I use “ps afxww” which shows even more. The ax flags show all of the processes, not just the ones that I started. The ww flags tell ps to display long lines instead of truncating them.

Note: The mac version of ps does not support the f flag. In this case, use the program pstree instead. It can be installed via MacPorts.

lsof shows open files

lsof lists all open files on the system. This is useful for determining what is reading or writing to a certain file. For example, if you can’t unmount a drive because it is busy, run the following command to see what is still using the drive:

% lsof | grep /media/usbdisk

lsof can even be used to see what is listening on a given port. For example:

% lsof | grep 3000
ruby      6126       paul    5u     IPv4      25036              TCP *:3000 (LISTEN)

There is a ruby process (with process id 6126) listening on port 3000. Now, we can use ps to see the full process information:

% ps ax | grep 6126
 6126 pts/1    Sl+    0:26 ruby ./script/server