Where I write code.

Monday, 12 April 2021

How to create Alias command in Linux or Ubuntu

Create Alias Command in Linux or Ubuntu




As a Linux user, you may come in a situation where you need to use the same command again and again. So typing the same command again and again will make it boring and sometimes it may happen that you will get distracted because of it.

So you can create an alias for the command to overcome this problem. Alias is like creating a custom or your own command, when you type your alias it will replace the actual command,

Let's take example of Alias in Linux:


As I am working on the MEAN application, mostly I need to run the frontend, backend, DB Services which is located in a different location so every time I need to type some commands to run the applications.

To Run the frontend application I need to type the following command.

 $ cd Documents/Work/myapp/frontend/

 $ npm start 


To Run the frontend application I need to type the following command.


Syntax to create an alias in Linux:

 $ alias aliasName="your command"



So let's try to fix create an alias for the above command

  $ alias myapp-frontend="cd Documents/Work/myapp/frontend/ && npm start"

as you can see if I just enter the following command on my terminal I can directly start my frontend application, but the problem is above command will be temporary.

Creating Permenanat Alias:

To save the alias we need to update the bash profile.

So now open bashrc file using any one of the terminal editor, I am using nano here, if you are familiar with vim then you can use that as well.

 $ nano ~/.bashrc


Once you open this file just go to end of the file and add the above custom alias as follows:


alias myapp-frontend="cd Documents/Work/myapp/frontend/ && npm start"


and then we need to update the file.


 $ source ~/.bashrc


And here we are you are able to save the alias for your command.

To remove the alias you can use unalias command 

 $ unalias myapp-frontend 




Conclusion

You can create an alias for the commands which you feel take time and it's repetitive work to do.

No comments:

Post a comment