Most people are familiar with the concept of an alias, whether it’s in the context of a nickname or a false name used to conceal someone’s identity. In the world of Unix, an alias is a bit different but no less useful. An alias is a command that can be used in place of another command or set of commands.
Creating an alias on a terminal can save you time and typing. This can be very beneficial if you find yourself typing long or complex commands frequently, and it can also make complex commands easier to remember. You can even use aliases to create custom commands that don’t exist by default.
Both the Bash shell and the Zsh shell support aliases. In this article, we will take a look at how to use aliases in Bash and Zsh, with some examples of common aliases to get you started. If you are not sure what shell you are using, most Linux distributions use Bash by default and if your using a Mac then it’s highly probable you are using Zsh.
You can check your current shell with the command below.
echo $SHELL
Creating Temporary Aliases
You can create a temporary alias by using the alias command followed by the name of your alias and then the command you want it to represent. This alias will only exist for the current session and will not be saved when you close your terminal. This is because temporary aliases are not stored in any configuration files, only in memory.
The syntax for creating a temporary alias is as follows.
$ alias alias_name=’command’
$ alias name=’/path/to/script’
Here is an example of how to create a temporary alias for the ls command:
alias l='ls -l'
Now, every time you type l in your terminal it will be the same as typing ls -l.
Remove an Alias
If you decide you no longer want to use an alias or maybe you created it by accident, you can remove it using the unalias command. The syntax for removing an alias is very simple, as you just need to type unalias followed by the name of the alias you want to remove.
For example, to remove the temporary alias l that we just created earlier, we would use the following command.
unalias l
Now, if we try to use the l alias again it will no longer work because it has been removed.
Another way to use aliases is for long or complex commands that you often type. A good example of an extremely long but useful command is the code displayed below. This command will find the top largest files hogging up space in a users home directory :
find $HOME -type f -size +200M 2>&1 -exec du -hs {} + | sort -rh | head -n 5
Having to type out the entire command every single time you want to use it can be tedious. A more convenient method would be creating an alias for the command, so you only have to type a few letters instead.
alias hf='find $HOME -type f -size +200M 2>&1 -exec du -hs {} + | sort -rh | head -n 5'
Not only is this quicker to type, given that the h and f keys are both on the home row of a keyboard, but it’s also easier to remember; “hf” could be a mnemonic for “hog files.”
Any aliases created during a session will be erased from memory once the session is over or the terminal window is closed. To create an Alias that lasts longer, you’ll need to take some extra steps which we’ll go over next.
Permanent Aliases
A permanent alias is an alias that you create and store in a configuration file so that it exists every time you open a new terminal session. As we mentioned before, both Bash and Zsh support aliases and each has their own configuration file where aliases can be stored.
For Bash the file is ~/.bashrc and for Zsh the file is ~/.zshrc.
To create a permanent alias you need to edit the file corresponding to the shell you are using and add a line with the following syntax.
alias alias_name=’command’
For example, to set up a permanent alias so the rm and mv commands always include the verbose and interactive flags by default, we would add the following lines to our ~/.bashrc or ~/.zshrc files. This is especially useful for new users, as it serves as a safety net to prevent them from inadvertently deleting files and folders. add the following line to the bottom of your ~/.bashrc or ~/.zshrc file
vi ~/.bashrc
alias rm='rm -iv'
alias mv='mv -iv'
Once you add the lines, save and close the file. To finalize changes, source the file or open a new terminal window.
You can verify that the alias was successfully created and is now part of your environment by using the alias command again.
alias
As you can see, the rm and mv commands now have the verbose and interactive flags set by default.
In this article, we covered what an alias is and how to create, remove, and make permanent aliases in both Bash and Zsh. Aliases can be extremely helpful when working in the terminal by reducing the amount of typing required for frequently used commands or simplifying complex ones.
Be sure to look out for the next article, which will introduce you to creating a separate alias file. In addition, you’ll learn about some common aliases that every new user should know. In the meantime, try creating some aliases of your own to help streamline your workflow. Soon you’ll be able to work more efficiently and get the most out all the features available through the terminal.





This was a very helpful article, I hope you continue to make more!!! I’ll have to try creating aliases, thank you!