Linux Commands

How to Create and Remove Alias in Linux

In Linux, the alias is a command that allows us to execute multiple commands or operations by creating a shortcut. It saves time and improves productivity especially if your work involves typing numerous commands.

For example, if you want to create a directory and a file inside that directory then you have to execute different commands to achieve this task. However, aliases help in doing such complex tasks in one go.

In this guide, I will explore, what are aliases, their types in Linux, and how to create and remove aliases.

Requirement

 

System Linux (Any Linux distribution)
Access Root/sudo access to the system

 

What is an Alias

In Linux alias is a command line utility that can create a shortcut for multiple commands or operations. An alias reference to the group of commands that run simultaneously.

In Linux, all the commands are hard to remember and with the nature of the operation, the usage of the command also gets complex. The alias command is mostly used to replace long and complex commands with shorthand so that any command or option errors can be avoided.

Syntax of Creating an Alias in Linux

Use the following syntax to create an alias in Linux.

Syntax:

alias name=’<commands…>

 
In the above syntax:

alias: The keyword for making an alias.

name: The alias name, it can be any name.

<commands>: It includes commands or groups of commands. It can also include options and other arguments.

Some important considerations for creating an alias:

    • Give a unique name to the alias and while creating a permanent alias ensure it does not match any predefined commands.
    • Use single quotes to include the commands.
    • Avoid adding space after and before the equal (=) sign or it may give an alias not found error.

How to Create an Alias in Linux

An alias can be created using the alias command and the syntax given above. Let’s understand it with an example:

alias update=’sudo apt update && sudo apt upgrade’

 

I have created an alias update of two frequently used commands in Linux; update and upgrade. Instead of typing two commands, you create an alias and type only that alias to perform the operations.


Let’s understand it with another example:

alias move=’cd ~/Documents/new_documents/latest/files/

 

In this example, I have created an alias move to navigate the files directory. Instead of typing the entire path, I can use alias move to directly get into the files directory.

How to List Aliases in Linux

To list the aliases in Linux, type the alias command and all the aliases will be listed.

alias

 

Types of Aliases

There are two types of aliases:

Temporary Alias: The temporary alias remains in operation as long as the current session is active and gets deleted automatically when the session ends. The temporary alias is simply created using the alias command.

Permanent Alias: The permanent alias remains in operation even after ending the session. The permanent alias requires some additional changes in the system files.

Create a Temporary Alias

Every alias that is created using the alias command is temporary. It remains operational as long as the session is active. For example, let’s create a temporary alias that updates the repositories.

alias update=’sudo apt update && sudo apt upgrade’

 
It will work in the current active session. Now exit the session and log back in, try to run the alias and it will not work.


To exit the session simply close the terminal and launch it again.

Remove a Temporary Alias

To remove the temporary alias, use the unalias command while being in the active session.

Syntax:

unalias <alias-name>

 
For example, to remove the update alias use:

unalias update

 

Now, list the aliases and it can be seen that update is no longer available:


To remove all the aliases, use:

unalias -a

 

Create a Permanent Alias

To create a permanent alias, you have to make changes in the shell configuration file. The configuration file depends upon the shell you are using.

    • For Bash it is bashrc
    • For Zsh it is zshrc

I am using Bash; therefore, I will open the bashrc file.

sudo nano ~/.bashrc

 

Now, type the alias update at the end of the file. The alias would be:

alias update=’sudo apt update && sudo apt upgrade’

 
Save the file using ctrl+x and then press y/Y.


Now, source the file:

source ~/.bashrc

 

This alias will remain permanent whether you end the session or turn off the machine.

The redirection operator (>>) can also be utilized to make the permanent alias as it will append the alias command at the end of the bashrc file.

echoalias update=’sudo apt update && sudo apt upgrade’” >> ~/.bashrc

 

Don’t forget to source the bashrc file to save the changes.

Remove a Permanent Alias

To remove a permanent alias you need to remove it from the shell configuration file. In my case it was bashrc, open the file and remove the alias.


After removing the alias, source the bashrc file by executing the command given below:

source ~/.bashrc

 
The alias has been deleted.

Create an Alias with Arguments

Aliases become even more useful when you are able to add arguments to them. You can do it with the permanent alias creation technique.

Note that in this technique, we do not use the alias keyword, we will use a function instead.

Syntax:

function <function-name>(){
<commands…>
}

 
Let’s create a function that will take the filename as an argument and create a file in the current working directory.

In the following code, $1 is the argument; the number of arguments can be increased by using $2, $3 and so on.


Open bashrc file and type the following function at the end of the file.

function createFile(){
      touch$1
}

 

Now, run the source ~/.bashrc command to save the changes and enable the alias.

source ~/.bashrc

 
Now, run the alias function with the file name.

createFile myFile.txt

 
A file will be created with the user’s given name.

Remove an Alias with Arguments

The procedure of deleting the alias with arguments is similar to deleting a permanent alias. Open the bashrc file, remove the function; save the file, and source it using the source ~/.bashrc command.

Conclusion

The alias in Linux is a useful utility that allows you to create a shortcut referencing a command or multiple commands to operate. Saving time, and improving efficiency are the key advantages of this command. Aliases are temporary but they can be made permanent by editing the shell configuration file. Temporary aliases can be deleted immediately or they automatically go away on exiting the active session. While for permanent aliases you need to delete them from the shell configuration file bashrc or zshrc.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.