Random Linux

Linux, video games and web hosting

Ever wonder how you can make shortcuts for some of those long cumbersome Linux commands?  You can by making an alias for it.  What is an alias?  It is a shortcut, let me demonstrate.  Say you want to restart Gnome Network Manager.  To do that you would type the following in a terminal:

sudo /etc/init.d/networking restart

Now you could make an alias called
netre to save yourself some typing and this tutorial will show you how.

Remember this tutorial is for BASH and not SH, CSH, Korn, etc. and more specifically for use with Ubuntu.  I cannot guarantee that this will work with other Linux distributions.

The first thing you will need to do if open your .bashrc file located in your home directory.  To do this type the following in a terminal:

gedit ~/.bashrc

This will bring up the Gedit text editor with the contents of the bashrc file.  Netx step is to locate the Alias definitions in the file.  You will need to uncomment the if statement to activate the alias definitions in a file or just add them directly in the .bashrc file.  The cleaner approach is to add them in a separate file for separation.  We will proceed with putting all your aliases in  separate file.  As you can see from the example listing I have uncommented the if statement to put all my definitions in a separate file.  Once this is done seave and exit gedit.

# Alias definitions.
# 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

The next step is to create a new file called .bash_aliases in your home directory.  You can substitute another name other than .bash_aliases if you like.  To create the new file type the following in a terminal:

gedit ~/.bash_aliases

This will once again bring up the Gedit text editor with a blank text file.  We can now sart adding aliases to the file.  The format for adding an alias is as follows:

alias <desired alias>='<linux_command>’

Here is an example:

alias netre=’sudo /etc/init.d/networking restart’

Make sure you use the tick mark ‘ when enclosing the linux command.  Once you are done adding the command hit the “Save” button.  Now the next time you bring up a terminal and type netre you will restart network manager.

You can add numerous alias commands to the file for your command line happiness.

March 21st, 2013

Posted In: General, How To

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *