Menu

Monday, 27 June 2016

Alias is a great way to define a command to do a particular tasks. To understand the usefulness, relate it to this: the normal rm command won't ask for a confirmation message if you want to remove a file. This may sometime lead to removal of some files by mistake and you wish at that time that there should have been a confirmation message warning. Now, you can always use "-i" with rm to ask for a confirmation each time you run the command, or you can create an alias for rm which should execute the command rm -i.

This was the first importance. Another benefit of using alias is to create your own commands. If you want to use ls for the equivalence of ls -lart, you can create on such alias. Also, if you want to create a whole different command say xxx to display files, you can alias xxx to ls.

Creating alias is very easy in Linux. I am showing an example of aliasing ls -lart with ls so that each time you use ls, the output shown will be that of ls -lart.

The first method is to define the alias in the terminal. Type in the following command in the terminal:

alias ls='ls -lart'

Now, each time you type ls and press enter, you actually executed ls -lart command.

But, this is a temperory solution and as soon as you close the terminal, the alias is destroyed and you need to create it again in your next session.

There is another way to create a permanent alias in Ubuntu. For creating a permanent alias, you need to make changes in the file ~/.bashrc and execute it once for the user.

To edit the file, type gedit ~/.bashrc in your terminal and add the following line at the bottom of the file.

alias ls='ls -lart'

Save and exit. Now type in the following command in the terminal,

. ~/.bashrc

and you can use ls to see the output of ls -lart. You can also use the VI editor to edit the file and make changes.

You can see in the above screenshot how ls command is giving an output equivalent to ls -lart. I have made the changes for root user. This alias will work even after you close the terminal or restart your system. This is how you create a permanent alias in Ubuntu.