Unix How-To: Time-Saving Aliases

Unix users are always looking for ways to make their systems a little easier to use or themselves more productive. In today's column, we'll look at a series of helpful aliases that might preserve a few of those brain cells for harder tasks.

The first alias, which I am just calling "today", displays the current date in another format. While it's easy to type "date" anytime you wanted to be reminded whether it happens to be a Thursday or a Friday, it's a bit more painful to print the date in your favorite format whether to remind yourself or to write into various log files.

alias today="date +'%d %B %Y'"

This alias displays the date in the "21 July 2010" format, but read the man page for the date command and you can rebuild the command to use the format that you prefer.

Another alias that I have found useful over the decades is one I like to call "ruler". It just displays column positions for me on those occasions when I really need to know what column a particular character falls in or I want to gauge the length of a few lines of text.

alias ruler="echo .........1.........2.........3.........4.........5.........6.........7.........8"

An alias I often suggest to other admins as well as to Unix users is one that removes world permissions from directories.

alias fixdirs="find . -type d -exec chmod o-rwx {} \;"

This alias uses find to locate directories within the current directory and uses the chmod command to remove read, write and execute permissions from the "other" group.

Getting the right netstat command can take a few passes over the man page or you can just create an easy alias. One or both of the following might work for you. The first (Linux version) lists open ports. The second displays listening ports on both Linux and Solaris systems.

alias openports="netstat -nape --inet"
alias listening="netstat -an | grep LISTEN | awk '{print $1}' | sort -n"

The next alias lists files on a Linux system with color coding to differentiate file types.

alias ls='/bin/ls --color -F'

This next alias, which I call "again" but you might think of a much better name for, will open the most recently updated file in a directory using vim.

alias again="vim `ls -t | head -1`"

If you just want to quickly list the most recently updated file, you can use this alias.

alias prev="ls -t | head -1"

The following alias has helped a lot of people track down the data hogs in their own files. It uses a mix of find, le, sort and head to list the five largest files in a particular directory.

alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"

/u/faculty/shs> findbig
484 ./tmp/.temp.txt.swp
196 ./CSI135files.tar.gz
156 ./tmp/.file.swp
76 ./dumdir/passwd
60 ./date

The next alias is not a big time saver, but might just tickle someone's fancy. This alias lets you type "up" when you want to go up one level in the file system -- a 60% reduction in typing over the standard "cd ..".

alias up="cd .."

And for anyone who frequently logs into other systems, especially if they need to switch usernames in the process, aliases like this (one for each target system) can save them a little time and annoyance.

alias server_name='ssh -l myself'

If you use a bunch of aliases and want to see how a particular alias is defined, you can get a listing with the alias command itself. Just type alias followed by the name of the particular alias.

$ alias up
alias up='cd ..'

If you sell your users on a nice collection of aliases, you might also want to tell them how to turn them off when they don't want the particular behavior.

unalias ls

Sometimes you can make a Unix user happy just by giving him a tool that saves him a little time. Sometimes the time you save may be your own. But good use of aliases can always make a day's work feel a little less tedious.

This article is published as part of the IDG Contributor Network. Want to Join?

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon