How to create an alias in Linux
As system administrators and command-line enthusiasts, we inevitably find ourselves using the same commands ad nauseam. If we are any good at our jobs, we should always strive to make our processes more efficient. The goal is to ultimately "do more with less."
One of the first articles I wrote for Enable Sysadmin, which you can find here, was about the AutoHotkey tool for Windows. This is a fantastic utility that allows you to set up a user-friendly script that lightens your workload. As I have yet to find a suitable substitute for this tool in Linux, I looked into setting up aliases for the commands that I use regularly. Since I am no longer working in the support sector, most of these are just quality of life changes. Once you understand how to configure an alias, this process can be applied to almost any situation.
Aliases
First things first: What is an alias, and why do I care? Well, an alias is a custom command created by the user to execute another, usually more complicated command or group of commands. The utility of aliases can be seen in 'Decluttering process management', where I mention an alias I created for the following command:
$ ps xawf -eo pid,user,cgroup,args
Instead of typing this full command, I created an alias that allows me to use pscgroup
to execute the original, much longer command. Now that you can see the utility on offer, let's take a look at where and how to configure our aliases.
[Want to try out Red Hat Enterprise Linux? Download it now for free.]
Creating aliases
There are a few things to note when creating aliases. For the purpose of this article, aliases are built on a user-by-user basis. If I create an alias as user tcarrigan
and then change to the root
user, the created alias will not work. Here you can see I aliased ls
to use a program called EXA that displays additional information and adds color-coding. The same command, in the same directory as the root user, displays the standard output for ls
:
There are locations where you can create aliases to span all users. For more information on this topic, check out the bash shell documentation.
For now, we will create our alias under user testuser
. So, in my home directory, I enter my alias into .bashrc
. Keep in mind that this file is hidden by default. It can be seen by adding the -a
flag to your ls
command:
You can see that this user starts with no aliases. We will correct this situation soon. For now, we will look at the command syntax. The correct syntax is as follows:
alias shortname=<command you want carried out>
We will now alias the ls
command to ls -lra
so that you get a much better look at all of your files, both hidden and non-hidden:
Now, reload the .bashrc
file using the following command:
$ source ~/.bashrc
When you use the standard ls
command now, you can see that the alias is now active, giving you the output for ls -lra
:
Wrapping up
While this alias is a simple quality of life change, you can apply this same concept to long and complicated commands that you might have to use routinely during your daily work. Let us know what some of your favorite use cases are. We at Enable Sysadmin look forward to hearing from you.
Tyler Carrigan
Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me