Tuesday, July 19, 2016

Alias Creation in Linux

We all use commands to do the stuff in any OS. We often face situations where we have to issue the same commands again and again in order to do the stuff.

For instance, we might face the situation in which we have to come to a particular directory and have to issue the commands. Also, there might be the case that the command issued have to keep running and because of which we have to open another terminal and have to issue other command but in the same directory in which the last command is issued.

For those type of things, there is a trick of creating the alias in Linux OS.

Now before going forward let me explain to you what does ALIAS means?
    In simple language, Alias is a shortcut to do some specific stuff.

Let's try to create one alias that takes you to the specific directory. For example, your working directory is ~/user/work. And you have to issue all the commands in this specific directory. In that you have to open a terminal and always have to issue a command that looks like this:
  • cd /home/user/work
For this we will create an alias named cdw (change directory to work, for instance).
  • Open the bashrc file for the user to whom you have to provide an alias for.
    • vi ~/.bashrc
  • Come to insert mode and add the following line at the end of the file with the following syntax.
    • press i to come to insert mode
    • Add the following at the end:
      • alias cdw='cd /home/user/work'
  • Save and exit the vi editor.
    • press ESC, then :wq then ENTER 
  • Exit the terminal and reopen new terminal to get the new settings.
The commands used are self-explanatory and tell that the name of the alias would be cdw which will redirect you to the /home/user/work directory.

Creating an alias on a Mac machine is very simple. Mac OS comes with a command named alias that can be used to generate a new alias and can be used to see the alias as well. Similarly, in order to delete an alias, you can use unalias.

But the alias and unalias command is temporary in nature and you can follow the above same procedure in ~/.bash_profile and add the alias you want to keep it permanent.

Using above steps will create an alias and makes your life a bit easier.

Happy coding to you all.  :)

No comments:

Post a Comment