Linux allows you to create aliases which are nothing but shortcuts to commands. They are very useful if you frequently use a command which is quite long and you don’t want to type it every time. But these aliases get lost after system reboot. In this article, we will look at how to create permanent alias in Linux that remain even if you reboot your system.
How to Create Permanent Alias in Linux
There are two ways to create permanent aliases in Linux. We will create bash aliases for our example, that work in bash shell. You may create them for other shells as per your requirement. The following steps can be used in almost every Linux distribution.
1. Using .bashrc
.bashrc file is loaded every time you reboot your system. So any command added to this file will be automatically run at reboot. So we will simply add the commands to define aliases in this file, thereby making them permanent.
Here is the syntax to create alias
alias alias_name = command_to_be_aliased
Here is an example to create alias
sudo alias list_all='ls -all'
Open ~/.bashrc file in a text editor.
$ sudo vi ~/.bashrc
Add the following line to its bottom
sudo alias list_all='ls -all'
Save and close the file. Now whenever you reboot the system, the above alias will be automatically created and available for use.
$ list_all
2. Using separate file
If you don’t want to change ~/.bashrc frequently, just create a separate file, say, ~/.bash_aliases
$ sudo vi ~/.bash_aliases
Add all the aliases you want to create.
alias list_all='ls -all'
alias list_long='ls -l'
alias cs='cd;ls'
Save and close the file.
Open .bashrc file
$ sudo vi ~/.bashrc
Add the following lines in it.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The above lines basically, load .bash_aliases file if it exists. Now, whenever .bashrc file loads during system reboot, it will automatically load bash_aliases.
That’s it. In this article, we have looked at a couple of ways to create permanent alias in Linux. Of these, the second method is recommended as it separates aliases from the important ~/.bashrc file. Also, once you have created /.bash_aliases file you can keep adding more aliases to it, without any additional configuration.
Aliases are very useful feature in Linux that allow you to easily create shortcuts to frequently used commands. In fact, you can even create single alias for running multiple commands. The key to creating permanent alias is to put them in a file such as .bashrc that is loaded every time your Linux system boots, or put it in another file, and load it via .bashrc.
Also read:
How to Give User Access to Folder in Linux
How to Install Swift in Ubuntu
How to Install Monit in CentOS Linux
How to Install mod_wsgi in Apache
How to Install Memcached in Ubuntu
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.