How to Create Permanent Alias in Linux

Page 1

Often Linux developers and system administrators need to create shortcuts to Linux commands. They are very useful if you have long commands that you need to use frequently but do not want to type the entire line every single time. Linux allows you to create aliases for this purpose. There are two types of aliases –temporary and permanent. Temporary aliases last only as long as your terminal session whereas permanent aliases last even after system reboot. In this article, we will learn how to create permanent alias in Linux.

How to Create Permanent Alias in Linux

You can create permanent aliases in two ways. We will look at both these methods one by one.

1. Using .bashrc

In this approach, we place the command for alias in any of the files that are automatically loaded every time during system reboot. This will make them permanent. For example, .bashrc file is loaded on every system reboot.

Here is the syntax to create alias in Linux.

alias alias_name = command_to_be_aliased

Let us say you want to create an alias for the command ‘ls -all’.

sudo alias list_all='ls -all'

Open .bashrc file in text editor.

$ vi ~/.bashrc

Add the following line at the end of the file.

sudo alias list_all='ls -all'

Save and close the file. Now whenever your system boots .bashrc file will be loaded every time. Thereafter, you can call ‘ls -all’ command using the following alias.

$ list_all

2. Using Separate File

If you do not want to play with any of the system files such as .bashrc or you need to create too many aliases, then you can create a new file such as .bash_aliases.

$ vi ~/.bash_aliases

You can add all the aliases you want to create, one on each line, as shown.

alias list_all='ls -all'

alias list_long='ls -l'

alias cs='cd;ls'

Save and close the file. Open .bashrc file.

$ vi ~/.bashrc

Add the following lines to it.

if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases

fi

The above line basically loads .bash_aliases file whenever .bashrc file is loaded, that is, on system reboot.

Among the two methods discussed in this article, the second one is recommended since it separates the aliases from important files like .bashrc. It also allows you to add more aliases to .bash_aliases file, without any additional configuration or without editing .bashrc file.

In this article, we have learnt how to create permanent aliases in Linux. You can use these methods in almost every Linux distribution. Originally published at https://techosha.com/how-to-create-permanent-alias-in-linux/

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.