• Save time in Linux with bash_aliases
  • Save time in Linux with bash_aliases
    Written by battye
    Sat Jan 22, 2011 7:12 am
     


    Recently I installed LAMP under Ubuntu and the kind folk in the #ubuntu IRC channel gave me some fantastic - not to mention time saving! - advice.

    If you have any long commands you can easily shorten them through the use of aliases.

    For instance, with LAMP the command to start Apache was the long winded "sudo service apache2 start". A similar command is required to stop Apache.

    This is how to make an alias.

    In your Terminal window (Applications -> Accessories -> Terminal) see if the file ~/.bash_aliases exists. You can do this by typing in the following command:

    vim ~/.bash_aliases


    If the file exists you should see some contents in it, if not then you can create the file yourself (press the 'I' key to enter insert mode so you can enter text).

    Then enter the following contents:

    alias a2stop="sudo service apache2 stop"
    alias a2start="sudo service apache2 start"


    In the example above, this will allow you - the user - to substitute a2stop and a2start for the long winded commands. Of course, this only applies to users running LAMP. Alias can be used for a multitude of reasons. For example, another set of aliases could consist of:

    alias databaseshell="./script.py databaseshell"
    alias postgresall="./script.py postgresall"


    In that example, the alias would be used to quickly run a script with a certain command line argument. This could make life very easy if you need to run a script regularly.

    Now save .bash_aliases

    What if my alias doesn't work straight away?

    There are a couple of things you can do. Firstly, run these commands (the source command essentially forces the system to re-evaluate the contents of the file):

    source ~/.bash_aliases
    source ~/.bashrc


    You could also try closing and re-opening the terminal window, or perhaps logging in and out of the system.

    If typing your new alias still does not work, ensure that in .bashrc the following code entry is not commented/hashed out (otherwise the .bash_aliases file will not be read by the system):

    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
    fi


    Note: on an unrelated note, if you are experiencing issues with mod_rewrite with LAMP on Ubuntu, run the following commands to make sure mod_rewrite is enabled:

    sudo a2enmod rewrite
    /etc/init.d/apache2 restart


    I hope this article helps you save time just as it has for me!
     
  • Article search
cron