December 23, 2024

Aliases when using Linux command line can shorten commands so that there is no need to type them in full.

They can be viewed using the following command :

alias

list of aliases present and active when using Bash shell

Adding them is simple, for example : to add an alias for the command uptime as u :

alias u='uptime'

What this will do is show the system uptime just by typing in ‘u’. However, this change is only for the current session. It won’t be present after a logout or reboot.

So to make it permanent, .bashrc needs to be edited. This is the file which stores the Bash shell configuration. So, first backup this file :

cp ~/.bashrc .bashrcbkp

making a backup of old .bashrc file

[~ is the same as  /home/username/ but saves time when typing ]. For more information on Linux command line, check out the tutorial series here.

Then it can be edited using a text editor (nano in this example) :

sudo nano ~/.bashrc

Scroll to the aliases section and add the alias entry over there.

adding aliases in .bashrc file

Once added, save the file. The changes will not be in effect though because the old .bashrc file is still in use.

So make the Bash shell use new one :

source ~/.bashrc

Then try the added alias, it will now be active :

using the new .bashrc file with the added alias

Similarly, various different aliases can be added. So, for example instead of typing sudo apt-get update everytime, use an alias like upd.

aliases for different commands

Other frequently used command like those for installing packages and more can be aliased too as needed.

alias for sudo apt-get install

All done.

By admin

Related Post