11 October 2006

Useful shell aliases

In a shell, an alias is a user defined command that will execute some other predefined, and usually longer, command. It's a tremendously useful command line feature that allows you take the longer commands that you use often and make them very short. I also use aliases to, in effect, redefine basic commands to always use the options that I prefer. For example, two commands I often use in FreeBSD to get a quick look at the network status of a box are "netstat -rn" to see the routing tables, and "netstat -an" to see the network connections. I might type these commands many times when there is a 'situation'. So, I've defined these aliases to give me some much shorter, time saving alternatives:

alias nsr='netstat -rn '
alias nsa='netstat -an | sed -n "1,/Active UNIX domain sockets/ p"'

These two lines are in the file "~/.bash_profile" and are executed for me whenever I login. The sed command that I'm piping the "netstat -an" command through just cuts off the domain sockets listing which, on a busy machine, is usually long enough to cause the network connections to be beyond a page/screen long. Then again, on a machine with a really large number of network connections, the output from "nsa" may still be too long for your terminal. No problem -- "alias" to the rescue again:

alias nsa='netstat -an | sed -n "1,/Active UNIX domain sockets/ p" | more'

I put a lot of importance on the modification time of a file. So, the basic ls command does nothing for me. I always want to see the modification times and I always(*) want to see the files in modification time order. Furthermore, if I copy a file, I want to preserve the modification time -- the content still hasn't changed yet. Since I pretty much always want these options, I will use the alias command to redefine these basic commands to suit my own tastes:

alias ls='ls -lt'
alias cp='cp -p'

Here are few more basic aliases that I've found to be very useful with short explanations for each:

#"change back" to the last directory you were in.
# Automatic shell variable, OLDPWD, is from bash.
alias cb='cd $OLDPWD'

#This one is good one to help standardize your command line across different OS's.
# I think it was "alias psa='ps -ef'" on Sun
#"process status all"
alias psa='ps auwx'

#"list directories", lists all of the subdirectories (names not contents) in the current directory
alias lsd='ls -d */'

#I quit after a few pings, because forever is a long time. :)
# (Also, notice 5 pings is more than a certain other OS's default 4. :D )
alias ping='ping -nc 5 '

# * -- did I say "always"? Once in a while I want to find a specific
# file name in very full directory and alphabetical listing is better.
#"list alphabetcial" (aliases will expand inside other aliases, hence the absolute path)
alias la='/bin/ls -l'

I've also written a second article on this subject, "More useful shell aliases." There we'll find that shell aliases plus grep equals a happy sysadmin -- and a decent cup of tea, to boot!

[ tags: , , , , , , , , ]



Post a Comment

<< Home