How to Use the alias command in Linux

Want to know how to use the alias command in Linux?

Users of Linux and its distros are familiar with the use of the Command Terminal. Users type in commands to perform multiple tasks that one can perform while using their computers. 

While doing so, some commands are used regularly. For example, $cd is a command that is used regularly to change the current directory. Typing these commands, again and again, can be annoying. 

In such cases, users would prefer a shortcut to perform the same task they want to do. For instance, maybe a small word or just a combination of letters to change directories would help out. It would save time and be more efficient overall. 

Luckily, Linux users have the choice of creating small shortcuts for their regularly used commands. This is done using the $alias command. $alias works in a way that is somewhat similar to using functions in programming languages. 

So, let’s see how you can make the best out of the alias command in Linux.

Syntax of Alias.

Before we dive into how to use the alias command in Linux, we should discuss its syntax. The syntax for the $alias command is straightforward in its usage. 

$ alias <shortcut name>=”<command>”

You can create aliases for every command that can be used in Linux. You also have the choice of creating aliases for a whole command line, such as $cd /home/username/documents, or an alias for the command only. However, the value of creating an alias for only the command is very limited. 

How to Create a Temporary Alias.

There are two types of shortcuts you can create using the $alias command. If you create a shortcut to be used in the current session only, that shortcut will be called a “temporary alias”. 

Creating temporary aliases is useful for the repeated commands that you know you will be using for the time being and not again. They can greatly reduce the amount of typing and time consumed.

To create a temporary alias, follow these steps.

  1. First, open the Terminal.
  2. Once opened, you can use the following command as a template to create a temporary alias for your choice of command.
$ alias direcA=”cd Downloads”
downloads

In the command above, we have chosen “direcA” as our shortcut for the whole command line. 

  1. To check whether it works or not, type in just the shortcut in your Terminal and execute it. You should see that the intended function has been performed. 
alias command in Linux
  1. Let’s have a look at another example. 
$ alias list=”tar -tf textfiles.tar.xz”
alias command in Linux

Here we have created a shortcut to list all the contents of the compressed folder “textfiles.tar.xz”.

  1. Again, we will run the alias to check whether it works or not.
list

This is how you can create a temporary alias in Linux and its distros. Keep in mind that the alias you will create in such a way can be used for the current session only. 

Before we move forward, let’s discuss NFS servers on Linux. NFS or Network File Sharing is a technology that allows you to access files present on another computer, provided both the computers are connected to the same network. 

This is pretty useful, especially if you are working in an office environment where you have to share multiple files and folders with your co-workers. If you want to learn more or better use NFS on your computers, have a look at the article mentioned above. It covers everything.

How to Create a Permanent Alias.

The second type of shortcut you can create using the alias command is the “permanent alias”. If you want to create a shortcut that can be used in every session you start, that shortcut will be called a permanent alias.

Permanent aliases can be created by saving the alias in the shell configuration file of your Linux system. Some of the shell files are.

  1. ZSH: ~/.zshrc
  2. Bash: ~/.bashrc
  3. Fish: ~/.config/fish/config.fish

To create a permanent alias, follow these steps.

  1. Open the Terminal.
  2. Once opened, type in the following command to open the shell configuration file.
$ nano ~/.bashrc
alias command in Linux
  1. Save the alias you want to create at any point in the file. You will use the same syntax as before.
$ alias shortcut name=”command”
alias command in Linux
  1. For example, we will be creating a permanent alias for the same command.
$ alias direcA=”cd Downloads”
  1. Now, “direcA” is a permanent alias that you can use in every session.
alias command in Linux

This is how you can use the alias command in Linux to create shortcuts for your preferred commands.

How to Check for All the Aliases Created by You.

Once you have created a lot of aliases, it can be difficult for you to remember each one of them. In such cases, it would help if you could look at all the aliases you created to remember the specific one you want to use at that time. 

Thankfully, Linux has provided us with a way you can list all the shortcuts you have created. All the aliases created are saved in computer memory, and you can view them using the following command. 

$ alias -p

The -p operator tells the system to print out all the saved aliases. 

alias command in Linux



How to Remove an Alias.

If you think there are shortcuts you created that are of no use to you anymore, you have the option to remove the shortcut you created. This is called “unaliasing”. This can be done using the following command.

$ unalias <alias name>

This article was a guide on how to use the alias command in Linux. We discussed the reasons why users might need to create aliases and shortcuts. Next, we talked about the two types of aliases (i.e., temporary and permanent) and showed you examples of how to create both of them. 

We hope that you now know what aliases are and how you can create them for your own benefit. If there are any further queries, please let us know in the comments. 

If this guide helped you, please share it. 🙂

Author

Leave a Reply

Your email address will not be published. Required fields are marked *