Aliases in every profile

From SaruWiki
Jump to navigation Jump to search

Aliases in general

One of the fun little things of Linux is the ability to create your own aliases for commands that you often need. You can set an alias like this:

alias more='less'

This has the effect of intercepting any command entries for more, substituting less. Thus the alias makes sure that when you type more, you get the less command executed instead. This works because the shell keeps the list of aliases at hand, and substitutes your specified aliases at execution time.
Note: if you also have the more command installed, then you cannot get to the more command any more. And note that - in a completely opposite fashion - your aliases are local to your shell, and are not passed down to programs or to other shells. Thus, a script that needs to run more will not get confused by your alias - it will not use less instead.

Should you want to know which aliases are in effect, simply run


Should you want to know what commands you have stored under a particular alias, just run that:

alias more

The above use of aliases is very trivial; it gets better if you add parameters to the alias. And to top it off, you can also use (environment) variables. For example:

alias ls='ls $LS_OPTIONS -lAF'

will make command ls use bot the value of $LS_OPTIONS (usually something like --color=auto) and the -lAF parameters.

Finally, you can have a single alias run multiple commands, separated with semicolons, pipes et cetera. Suppose you often want to know what processes are running with the credentials of web-user www-data:

alias psw='ps -ef | grep www-data'

Try it :-)

Aliases in profiles

Every time you set an alias, it is known only in that particular shell. Once you exit that shell, the aliases you've so carefully set are gone forever and ever! Of course, there is a way to preserve your work: set the aliases in one of your profile files (~/.bash_profile, ~/.profile et cetera). However, you can also put aliases in one of the system-wide profile files: /etc/profile or /etc/bash.bashrc. Especially the latter is suitable, as it is meant to hold functions and aliases. However, if you also want to have the aliases available when you log in remotely, you should either put the aliases in /etc/profile, or have /etc/bash.bashrc sourced from /etc/profile.

Our favorite aliases

This is a list of the aliases that we like so much that we put them on every one of our boxes, at the end of /etc/bash.bashrc (which we have sourced from /etc/profile):

eval "`dircolors`"
alias l='ls $LS_OPTIONS -lAF'
alias ts='multitail /var/log/syslog'
alias tf='tail -fn40 /var/log/ulog/syslogemu.log | ccze'
alias tm='tail -fn40 /var/appslog/mail/mail.log | ccze'

You can probably guess the meaning of the aliases from their commands, but in case not:

  • l - list in our favorite output form
  • ts - tail syslog
  • tf - tail firewall log
  • tm = tail mail log