Linux Toolbox: alias

Linux Toolbox: AliasI’m going to kick off the Linux Toolbox with a relatively simple one.

Command: alias
Purpose: create command-line shortcuts
Benefit: save time and effort by simplifying complicated, commonly-executed commands.

If you spend any time at the Linux command-line, chances are that you are going to type the same commands repeatedly. Or, there may be long or complex commands that you enter only rarely, but you don’t want to have to look up every time you need them.

Alias allows you create temporary command shortcuts within the current terminal session.

The syntax* is fairly simple:

alias ll='ls -l'

This example would create the shortcut ll, which would execute the command ls -l and give you a listing of your current directory in the “long” format. simple stuff. And, since it’s temporary, once you initiate a new terminal session, typing ll will probably result in a command-not-found error.

You can also use alias to create a shortcut for a series of commands:

alias distup='sudo apt-get update; sudo apt-get dist-upgrade;'

This follows the bash syntax of separating multiple commands with a semi-colon, and gives you a single command to execute what would normally require much more typing. This particular alias is probably the command i use more than any other, as I tend to run the upgrade pretty much daily.

Alias is great by itself, but its usefulness really becomes apparent when combined with the .bashrc file, which handles the task of setting up your bash environment each time a session is initiated. In fact, if you look at your current ~/.bashrc file, you’ll probably find several aliases already setup. you can type alias as a command with no arguments, and it will return a list of all the aliases in your current session, including those established in the ~/.bashrc.

You could also store your “permanent” alias definitions in a separate file, for ease of management. If your distro’s ~/.bashrc doesn’t include this block of code, you can add it:

# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases

Then create the ~/.bash_aliases file, add your new aliases (one per line), and copy over any existing aliases from ~/.bashrc.

Once you’ve added your new aliases, each new bash session you initiate will have your aliases setup and ready to run.

To activate them in the current session, reload your bash settings:
source ~/.bashrc

The major limitation of alias is the inability to create an alias using variables to pass arguments to the aliased command. while this does restrict some possible usages, it doesn’t negate the command’s ultimate utility.

Alias can be a huge relief for frequent command-line users, helping to simplify tedious commands, speed up entry, or make commands more easy to remember.

Every user has their own way of setting things up, of course. What command would you consider, like alias to be indispensable in your daily usage? If you’re already making good use of alias, what are your favorite or most used aliases?

Here are some of mine:
# retrieve and install launchpad PPA key to synaptics keyring
alias addlpkey='sudo apt-key adv --recv-keys --keyserver'
# cleanup synaptics packages
alias apclean='sudo apt-get autoclean; sudo apt-get autoremove'
# quick synaptics package install shortcut
alias apinst='sudo apt-get install'
# call exirename for ALL JPEG images in directory
alias exiren='exirename *.jpg; exirename *.JPG'
# send a task description to an inbox file
alias in='echo >> /home/username/inbox.txt'
# quick shortcut to launch a common ssh session
alias ssh01='ssh -p 25'
# edit a file as root, useful for system administration
alias svim='sudo vim'

* all examples are based on bash syntax, so YMMV.

4 Responses

  1. […] am constantly learning new things. For instance, I had never considered combining multiple commands into a single alias […]

  2. Skiz says:

    When dealing with multiple firewalls and you have keys set up, you can initiate all of the hops with something like this to grab a clean mysql console or anything else you need.

    alias devel=’ssh -t host1 ssh -t host2 ssh -t host3 mysql -hdbhost’

  3. glebeconnection says:

    hey, surprise, didn't realise i'd get to comment so easily. but i had a wordpress account… Good show, launchin a linux toolbox :-) Like the "hard copy" graphics too…

    #run fortune when you want it; mine's commented out in .bashrc
    alias 4tune='mint-fortune'
    # edit this .bash_aliases file
    alias bal='vi $HOME/.bash_aliases'
    # access a pen drive called disk
    alias cdd='cd /media/disk'
    # customise .bashrc
    alias custom='vi $HOME/.bashrc'
    # count down the days to World Cup Socccer's kickoff
    alias fifa='expr 161 – `date +%j`'
    # switch to GUI to open a file
    alias go='gnome-open'
    # use GUI to open the home directory; 'nautilus $HOME' is slower
    alias home='gnome-open $HOME'
    # quit firefox without the mouse notching up miles; also clean cache
    alias kf='killall firefox'
    # logout from the terminal
    lgt='/usr/bin/gnome-session-save –kill'
    # ls varies among distributions; show hidden files only
    alias lh='ls -la –ignore=[0-Z]*'
    # open Open Office Word Processor but skip splash screen
    oo='oowriter -nologo &'
    # see your internet data usage
    alias ppp='ifconfig | grep –color '*\$''
    # use python to do calculations or serious programming
    alias py='python'
    # shutdown the computer from where you are
    shut='sudo shutdown -h now'
    # open another terminal
    alias tt='gnome-terminal &'
    # today in history and more
    alias today='grep -h -d skip `date +%m/%d` /usr/share/calendar/*'
    # open an xterm; useful since it looks different
    alias xt='xterm &'

    what i was looking for was a way to run a file as root. eg. source ~/bin/file followed by "file" as root would work but i had sudo in mind.

  4. to answer my own question: make the file in your bin directory and link to it from /usr/bin and bash will find its name in the path:
    sudo ln -s $HOME/bin/filename /usr/bin/filename