Thursday, December 11, 2008

Daily aliases

If you spend any time working with the shell, you probably use many GNU or BSD utilities. One thing that distinguishes the newer free software versions from the classic Unix versions is that the free software programs are rife with additional options. Some of these options are so useful you may want to create an alias so you can use them all the time.

note: originally published December 12, 2005 on
updated for Mac OS X on July 27, 2007

A shell alias is a simple way to create your own custom command. To make your aliases available every time you open a BASH shell, add them to your $HOME/.bash_login file. For other shells, place them in the associated run control file. In OS X, the default BASH configuration doesn't include very much, really just a slightly customized prompt. When I start working on a new system, I immediately install my favorite aliases.

An alias is created using the following syntax:

alias name='value'

where name is the name of the new command and value is the command string that is executed. Note, if you create an alias with the same name as an existing executable program in your path, the shell will run the alias instead of the program.

To remove an alias from your current shell session, use:

unalias name

where name is an existing alias.

To see all the current aliases known to your shell, type alias with no parameters.

All of the following aliases were created and tested on OS 10.4.10 using the standard BASH shell.

List the most recently modified files and directories

alias lt='ls -alt | head -20'

Once this alias is loaded, typing lt lists the most recently modified contents of the current directory. The a and l options show all files including hidden files in a long listing format. Since I am usually interested in a file I have just changed, I limit the output to 20 lines with the head command.

List only subdirectories

alias lsd='ls -al -d * | egrep "^d"'

This shows only subdirectories of the current directory by using egrep to limit the listing to entries with the 'd' directory attribute.

Show the inode number for files in the current directory

alias li='ls -ai1 | sort'

This prints the inode number (file system block identifier) for each file, sorted in ascending order. The last option to ls in this alias is the numeral one, not the letter L. You might need the inode number for file system repair or to recover data from a damaged file.

Scramble a file using the ROT13 algorithm

alias rot13='tr A-Za-z N-ZA-Mn-za-m'

This shifts all letters in a file 13 characters up the alphabet, creating scrambled, but not encrypted text. Running ROT13 on the scrambled text restores it. ROT13 was used back in the day on newsgroups to politely hide potentially offensive text. I almost never run across ROT13 text these days which may be a sign of lost Net innocence. To use the alias, redirect a file to standard input:

keithw$ rot13 < /etc/motd
Jrypbzr gb Qnejva!

Fast directory navigation with pushd and popd

alias +='pushd .'
alias _='popd'

I tend to move around a lot on the command line. If I am working on code in a deeply nested directory, but need to go check some log files, I'll use the shell built-in pushd command to save my current directory location and popd to get back to the directory later. These aliases simplify the process by entering '+' before changing directories, and '_' to return to the directory later. You can push more than one directory on to the stack and pop them back off in reverse order. I used the underscore instead of the minus sign for popd because the minus sign is a reserved symbol.

Find disk space abusers

alias dusk='du -s -k -c * | sort -rn'

Shows disk usage of files and subdirectories in the current directory, sorted with the ones using the most disk space first. The report shows disk usage in 1K increments and the total for the directory at the top. Beware, if you start this command from the root directory or at the top of a deeply nested directory, it could run a long time. Here is a sample report:

 keithw$ dusk
1756 total
528 micro
444 articles
408 images
152 move
48 cgi-bin
36 inc
28 src
24 index.html
12 xref
8 send.php
8 feed.xml
8 css
Show all programs connected or listening on a network port

alias nsl='netstat -alnp ip | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'

Since netstat is used to look at all system info, you need to run this as root. It shows the process ID and program name of everything either connected or listening on a network port, including the sockets of the sending and receiving hosts. This is a great way to make sure no unexpected services are running in the background. Here is a sample report:

Proto  Local Address          Foreign Address        (state)
tcp4 *.* *.* CLOSED
tcp4 *.* LISTEN
tcp4 *.5432 *.* LISTEN
tcp6 *.5432 *.*
Combinations and Permutations

If you want to dig deeper into any of the options used in these aliases, check out the man/info page for the command. There are so many permutations of options and commands that you can sometimes stumble across a cool solution by studying the more obscure options in the man/info pages. What are your favorite aliases?