Unix: The art of being lazy

One of the most endearing qualities of Unix devotees is how determined they are to be as efficient as possible. Why type three characters if you can do the same thing with two? And one of the best ways to cut down keystrokes is to turn common commands into aliases.


There are two really good reasons to create an alias in your Unix account -- to reduce typing (and typos) when you need to use fairly long or convoluted commands and to make the commands you use easier to remember. If you're one of those people who works both on Unix and Windows systems, just having dir work the same as ls might reduce your frustration level.

Some aliases make moving around the file system easier.

alias ..='cd ..'
alias ...='cd ../..'

And that set, of course, you can expand on if you want to back up even further.

You can use "c" or "cls" (a la Windows) to clear your screen.

alias c='clear'

Listing your files is not difficult or tricky, but what if you sometimes like to see your file names colorized to identify their types and sometimes you'd rather not? You can set up one alias to list all and another to list all files. You can also do the same with detailed listings. Plus my favorite file listing option (most recently updated last) with or without color might complete the set -- or maybe this many options is over the top!

alias lc='ls --color=auto'
alias la='ls -la'
alias llc='ls -la --color=auto'
alias lla='ls -la'
alias ltr='ls -ltr'
alias ltrc='ls -ltr --color=auto'

Aliases can remind you where important files are located or keep you from having to remember. If you have important log files in a directory with a path like /home/oracle/jboss/server/default/deploy/myapp.ear/myapp.war/logs, you are likely going to want some way to access them without recreating that path every time. An alias is one way to do that:

alias jboss.logs='cd /home/oracle/jboss/server/default/deploy/myapp.ear/myapp.war/logs'

Other options include installing a symlink in your home directory that points to your deeply buried directory or to a particular log located there, but the alias shown above has the advantage of not caring where you happen to be in the file system when you get to be curious about its contents.

You could also create an environment variable that contains the path to your logs directory:

$ export JBOSSLOGS=/home/oracle/jboss/server/default/deploy/myapp.ear/myapp.war/logs

If you like colorized file listings, you might also like colorized grep!

$ alias grep='grep --color=auto'
$ grep words xyz
This option makes it easier to pick out words of interest from a file with lots of words and may come in handy.

You can use aliases to find the largest files from any starting point. Here we show only the largest five.

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

You can use aliases for commands you sometimes forget require you to use sudo:

alias shutdown='sudo /sbin/shutdown'

You can use an alias to create a quick backup of a set of files, make sure the file name includes the data.

alias backup='tar cvf ~/`date +"%m-%d-%Y"`.tar


You can also use an alias to group commands together that you want to run in combination. The effect is like putting the commands into a script, but invoking the same sequence of commands again and again becomes painless.

alias x='date;pwd;echo hello'

Some other aliases that I've found useful are a rmdir that does a recursive remove and one that lets you toggle between two directories
without having to remember "cd -". As useful as that trick is, I often ask myself "OK, so how is that done again?". Aliasing is to
prev or just p (for previous) seems easier.

alias rr='rm -rf'
alias prev='cd -'
alias p='cd -'

And, of course, one of the best alias commands is the unalias command. Knowing how to turn off an alias when it's getting on your nerves because you're tired of the colors or don't want to be asked 162 times if it's OK to remove a file in a list of files you don't need anymore.

unalias rm

A well placed alias can make working on your Unix systems that much nicer -- and just think of all the keystrokes (and maybe brain cells) you can save!

Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.

Photo Credit: 

flickr / Al Scandar Solstag

Join us:






Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Join us:






Ask a Question