Figuring out how to create aliases in Linux is part of the advanced practices you need to know as a Linux user. And we’re here to help you break it down.
Working on Linux is very different from working on Windows and macOS. Our work is carried out through the terminal, which functions as the system’s heart by executing a line of commands.
If you’re already familiar with it, you must agree that most Linux commands are minimal subsets of already existing commands. Depending on your purpose, you must frequently enter the same things over and over.
And because of that, you might have felt the need for an alternative for these commands. If so, we’re here to help you through that!
How to Create Aliases in Linux
Aliases are potent techniques in Linux and Unix-like operating systems that allow you to hone your command-line experience the way you want it. When using aliases, it’s easier to manage and reduce repetitive tasks, and configure custom commands with other available options that users always use.
Aliases enable you to set a bash shell functionality name and define it, which can be called upon by using the name you have assigned. With it, you can manage a shorter and more clearly defined set of commands.
The process of creating an alias in bash is fairly easy with the use of the following syntax:
If you wish to form a new alias for the bash within the session, follow these steps:
- Type in the “
- You then need to declare the name of the specific alias
- Assign(with equal to sign) the command you are looking to run once you call upon the alias
You must ensure that the command you enter is in quotes, without spacing around the “
=” sign. Each alias should be defined on a new command line since using spaces here will break the command.
We all Linux users are familiar with the commonly used command, “
ls”. We often use the “
ls” command with “
la”, which will provide you with an entire list of all the files and directories available. Finally, you can form a simple bash alias “
ll,” which can be used as the command “
ls -la” shortcut.
This can be done quickly by following the previous steps in the terminal window. So first, we type in:
alias ll=”ls -la”
Now the alias has been created and we can use “
ls -la” will give you the same result as the output. However, the alias defined “
ll” will be available only in the current session. If the current session is closed or you initiate a new session window through another terminal, the alias “
ll” defined above will no longer be available.
Further, into this article, we also will discuss how you can make the alias permanent.
In the system, some of the aliases are predefined. To know about the aliases list defined in the Linux system, the command “
alias” can be used with no parameters:
Here’s the entire aliases list on the Kali Linux distribution. However, they may have some differences based on the system’s distribution.
From the list above, we can see several other aliases out there for the “
Moreover, an entire aliases group provides color output to the “
grep” commands family. For example, if we type:
The system will interpret it as:
alert” alias is an indicator when a command is done. The alias is also an indicator of the command completion. The alert alias offers you a visual system alert displayed as pop-ups in your computer screen.
For instance, the “
sleep” command will put the system to sleep for five seconds. When that time has passed, the “
alert” alias will appear, and it will check the previous command response executed. It will then extract the previous command from the history file list, decide if the command was completed or not, and provide you with a system alert notification.
A tiny terminal window symbol will appear on-screen if the command completes as expected. However, if this command comes back with an error, the system alert icon displays a red error symbol.
As we already know how an alias is defined in the system, we can now go on to creating a clear command pseudonym. The alias can be named “
cls,” and can call the “
As we can see above, since it’s a bit complicated, we didn’t wrap the alias definition in quotation marks. If the alias body is more complex or contains spaces, it must be wrapped in single quotations.
With the alias, the screen was cleared. The alias can only survive no longer than the separate terminal window in question. After the window closes, the alias itself vanishes.
Using the “.bashrc” File for making Aliases permanent
.bashrc” file defines these pre-packaged aliases. This is a read file, and the commands inside the read file are executed whenever we create an interactive shell, and this is precisely when a terminal window needs to be launched.
To view all of the “
.bashrc” file contents along with syntax highlighting, type in the following command:
This will launch the gedit editor with the “
As you can see, the highlighted areas display two areas where the aliases are defined.
When you scroll through this document, you will see two other sections related to the aliases:
At the top is how you put down the definitions of the “
alert” alias. The second one mentioned is an “
if” statement. It translates to, “if the file “
.bash_aliases” exists, read it in.”
We can put them in your “
.bashrc” file. Tuck them in below the section containing the “ls” aliases. But if we want to create several aliases, they can be defined in our “
Creating them in the “
.bash_aliases” file prevents us from accidentally changing any of the “
.bashrc” file settings, and our aliases are quite effortlessly replicated to new systems because they are completely independent of the “.bashrc” file.
Store Aliases in .bash_aliases File
We can create the “.bash_aliases” file using the command below, as it doesn’t exist by default.
Now we can edit the file and add a few aliases to it using the gedit editor.
We have added three aliases, out of which, the very first one is the
cls alias that we used before. The next one is called h and is a shortcut for the history command. The third and final one is called
ftc, which is “file type count.”
If you got anything wrong or if you want to revert the changes, you can use a command that is designed to remove aliases while ensuring that the bash doesn’t respond to them or recognize them—called unalias.
For example, we can use the “
unalias ll” command to make bash forget the
We can use unalias to remove aliases that we have defined and also the predefined ones. To remove all of the aliases from the system, we can use the following:
However, this won’t be permanent, as those aliases will be back the next time we open the Terminal. We need to remove them from our “
.bashrc” and “
.bash_alias” files to wipe them out.
Keeping Track with type
It can be difficult to remember whether a particular command is an alias or a function. We can use the type command to remember its type. This can be done just by using the type functions:
Aliases and Shell functions can enhance the speed and productivity of the command line experience. They can shorten the command sequences and make things go in your own way as we can customize the command that we use—which can be boring and difficult when carried out repeatedly.
We can shorten the line of commands to make it easy to remember and execute in the terminal. So remember, the proper and systemic use of aliases can help make your user experience better than before.
If this guide helped you, please share it.