note: originally published December 12, 2005 on newsforge.com
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:
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:
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.
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.
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.
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!
alias +='pushd .'
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.
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$ duskShow 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)Combinations and Permutations
tcp4 192.168.1.137.63819 22.214.171.124.443 ESTABLISHED
tcp4 192.168.1.137.63817 126.96.36.199.443 ESTABLISHED
tcp4 192.168.1.137.22 188.8.131.52.4570 ESTABLISHED
tcp4 *.* *.* CLOSED
tcp4 127.0.0.1.1033 127.0.0.1.990 ESTABLISHED
tcp4 127.0.0.1.990 127.0.0.1.1033 ESTABLISHED
tcp4 127.0.0.1.631 *.* LISTEN
tcp4 *.5432 *.* LISTEN
tcp6 *.5432 *.*
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?