As we all know, there are a few Linux commands that are so important that as a Linux user, we just have to know them all by heart. Amongst these important commands is the alias command. In this tutorial, we will learn about the alias command.
Alias in the Manual (Man Pages)
The first thing any Linux user does (when faced with a new command) is to check the Linux manual or man page for the command in question. However, in this case, it will throw a “no manual entry for alias” error. So let’s first fix that before we learn about the command itself. Information about the alias command which is a “shell builtin” is available in the POSIX Programmer’s Manual. You can search for manpages as follows:
Install the POSIX Programmer’s manual as follows:
Next, we can type the following:
“Man alias” will chuck out all the information you need about the alias command (in the event that you need further info).
The Alias Command
Alias is used to define an alias (by definition). Basically, what that means is that you can replace one command with another. Suppose that you have a command that is long and a bit harder to remember. In my case, I hate trying to remember the tar command with its z, x, v, and f. And yes, there’s the manual but it’s really inconvenient for me to remember all that, so what I can do instead is to create what is called an alias. This alias will replace the command in question, but act exactly like it.
So let’s take the following:
The complicated part that I’d like to alias is “tar -zxvf”.
The syntax used to create an alias is as follows:
So let’s create an alias for “tar -zxvf”. In my case, I’d like to set the word “tam” as the alias for “tar -zxvf”. So I’d type the following:
Here, after the word alias, we put the word we want to use as the alias (in my case it’s tam), and then an equal sign and then in quotation, the expression you want the alias to perform.
So, if I have the same file as before “file.tar.gz”. Typically, we’d use “tar -zxvf” to unzip it. Now however, we’d write the following:
Both expressions are equivalent to each other or in other words, they are aliases!
Now, suppose that you choose to name your alias as the same as an existing command. Suppose that instead of “tam”, I choose the word “tar”. In such cases, it will overwrite the existing tar command with the alias tar command. So suppose I do the following:
Here, as we can see, the alias I’m using is also tar, so now the expression “tar” is equivalent to the expression “tar -zxvf”. So if I type:
It will unzip the file.
Now that you know how to create aliases, the next thing you need to know are the available aliases. What do you do to check what aliases are available? Well, check the list of aliases available by typing:
$ alias -p
It (alias or “alias -p”) will give you a list of the existing aliases, and that list will include the aliases created by the system for you as well. In my case, there are a few aliases created by the system for me: egrep, fgrep, grep, l, la, ll, and ls. Along with those created by the system, there’s the one created by me (tam, in the image below).
Now, the problem that I encountered was that you can create as many aliases as you want with the alias command but every time you restart the computer, all the aliases you’ve created will be erased. Mind you, only those aliases created by you will be erased; those created by the system will be available still. So back to square one. So what do we do? Well, in order for the command to be on your system, you need to edit the ~/.bashrc file.
Open the ~/.bashrc file, and uncomment the following:
# . ~/.bash_aliases
When you uncomment it, it means that you are agreeing to put the aliases in the ~/bash_aliases file. If you do not have a ~/.bash_aliases file, then create one. In the ~/.bash_aliases file, write the following:
And save the file. Once the file is saved, restart the terminal!!! If you do not restart the terminal, then it will not recognize the change! Now, you can once again use the alias tam to unzip tar.gz files, and this time, when you re-start your system, it will still be available on your system (it won’t be erased).
The next thing you need to learn to do is to undo the aliases. For this, we use the command “unalias”.
The syntax is as follows:
Ex: unalias tam
This will unalias the tam command temporarily. This is useful if you only want to create, use or undo an alias temporarily. If on the other hand you want to permanently unalias the command, then you have to modify the ~/.bashrc file. The command (tam in my case) will be located at the end of the file, erase it. Then restart the terminal once again.
The problem with some Linux commands is that they can be long, and tedious to remember. In such cases, we can use what we call aliases. Aliases, by definition, are another name for a particular command. Once created, temporarily or permanently, we can use the alias instead of the full long command. Mind you, if you want the alias to be permanent, then you do have to modify the ~/.bashrc file! Either way, the alias command is available to us to save time, and effort. You no longer have to type out very long commands!