Every Linux administrator is different. Every server is also different. A combination of these two means that no two server administrations are identical. Each will have different tools, different ways of doing things, and a unique set of routine tasks that must be carried out on a daily basis. Such an environment calls for tremendous flexibility on the part of the interface. We can’t have pre-set shortcuts that everyone uses, because the needs are so varied.
Often you will need to execute a very specific command that you have personally put together. It might be complex, and contain a lot of dashes, dots and other special characters. Typing it again and again can become annoying and time consuming. For these situations, Linux provides the convenient shortcut of “aliases”.
Example of an Alias
An “alias” in Linux is simply another name for a custom command that you type into the terminal. For example, let’s say you work often with the $PATH variable and need to display it using the following command:
This produces the output shown here:
As you can see, this produces a list of places where the system looks when a command is specified. The locations are on a single line separated by a colon (:). If you rarely look at the $PATH variable, this is fine. But if it’s a daily part of your job, you probably want a “prettier” output. To do this, you can use the following command:
sed 's/:/\n/g' <<< '$PATH'
This gives us everything on separate lines:
Much cleaner! “sed” is an excellent tool for filtering output and you can see its power here in just one command. I’d written earlier about using sed to append text to files. However, its documentation is pretty cryptic!
And yet, could you imagine typing sed ‘s/:/\n/g’ <<< ‘$PATH'” day in and day out? I couldn’t! Too much effort. This is a perfect example of where an alias would be useful. In the following command, I’m going to create an alias called “path” and link it to the above command:
alias path="sed 's/:/\n/g' <<< '$PATH'"
Now instead of typing the complicated command, I just need to type “path”. Like this:
That’s much better! However, there is a catch. This is only temporary. The next time you start up bash, your changes will be overwritten and you’ll get a “command not found” error. Like this:
This is quite a common thing with Linux configurations. There are lots of commands to change settings, but in order to make them permanent, we usually have to make changes to some file somewhere. Similarly, to make the alias permanent, we’re going to have to modify something known as the “bashrc” file.
How to Make Aliases Permanent
Making aliases permanent means that we have to modify the “bashrc” file located in the root of your profile directory. Here is the location:
Using an editor like “vi”, open it up and go to the very end of the file. Then add the regular alias command. Here’s a screenshot:
Save and exit. The changes to the bashrc file won’t take effect until you reload it. You can do that by using the following command:
And now, you can see below that the “path” alias works as intended. Not just immediately, but also after I’ve reloaded the bash environment as I did before:
That’s how we create an alias for existing command line operations so that they’re easily remembered. I doubt if there exists even a single long time Linux admin who doesn’t have their own set of customized aliased shortcuts. Even developers using a code version control system like git will have aliases for commonly used commands like “git fetch” and “git pull”!