Ubuntu allows you to customize your long and rugged bash commands by using short and pleasant aliases as a replacement. When you are working on the command line, you can just use an alias instead of the entire command you want to avoid typing. The alias will work exactly in the same way as the command it has been created against.
In this tutorial, you will learn how to create and use an alias against a command in Ubuntu. We will make use of a simple example to demonstrate this process for you. We are running the steps and commands described in this article on a Ubuntu 18 system.
Example: Setting up an alias for the – sudo apt-get install – command
If you do a lot of installations on your system and wish to avoid using the entire sudo apt-get install command, you can create a short alias for it using the following method:
Through the file manager, open the .bashrc file located in your home folder. This file is usually a hidden file so you will need to use the Ctrl+H control to view all the hidden files located in the folder. You can use your favorite text editor through the command line or UI to edit this file. We are using the default graphical text editor to open and edit this file.
Move to the end of the file and paste the following line:
alias agi='sudo apt-get install'
Here “agi” is the new alias we are setting up.
Syntax for creating an alias:
You can also add other aliases to customize your apt-get commands by adding the following lines to this file:
alias agr='sudo apt-get remove'
alias agu='sudo apt-get update'
alias acs='apt-cache search'
Save the file by clicking the Save button located at the top right corner.
Open the Terminal through Ubuntu Dash or by pressing Ctrl+Alt+T
Run the following command in order to start using the new bashrc file.
$ source ~/.bashrc
The new .bashrc file is installed every time you log out and then log in. The above command enables you to use the changes without restarting your system.
The alias has been set-up; you can now run the following command in order to install a new package to your system:
$ agi [package name]
$ agi nautilus-admin
$ sudo apt-get install nautilus-admin
You can see how in the above image I was able to install Nautilus by using the new command alias that I set up in this example.
Points to consider
While creating an alias please note the following points:
- You cannot use an already existing command as an alias name. If you do this, your alias will not work, instead, the default ubuntu command will be executed.
- The alias name cannot contain any spaces. If the new alias you want to set up contains two or more words, you can use the ‘-’ character to separate those words.
Now you can get rid of the dry and rugged bash commands and use your own customized aliases to run the frequently used operations.