In Linux, the alias command is used to create shortcuts for long commands that have the same functionality as if you were writing the whole command.
This way, you can improve your productivity and save time by creating a few aliases for the long or multiple commands (with or without options) that you use often.
Tutorial Details
Description | Alias (Keywords that are Replaced by a Command String) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | alias, vim, nano |
Internet Required | No |
Syntax of the Alias Command
The alias command takes two parameters: one is the option, and the other is the alias in “[NAME=VALUE]
” format.
$ alias [OPTION] [NAME=VALUE]
Listing the Existing Aliases in Your System
The alias command without any options or with the “-p
” flag will print all the defined aliases in your Linux system.
$ alias
Output:
As you can see, executing
$ la
is equivalent to:
$ ls -A
Note that all of these defined aliases are reusable and can be modified later.
View the Command Associated with a Specific Alias
The alias command without any flag will give you a list of all defined aliases in your system; however, you can also view the command associated with a specific alias using the alias name.
$ alias la
Output:
Another example:
$ alias ll
Output:
This might come into use when you have a long list of aliases and want to find commands associated with specific alias.
Creating Aliases in Linux
In Linux, there are two ways to create aliases in your system:
- Temporary Aliases
- Permanent Aliases
Let’s start with:
Creating Temporary Aliases in Linux
Temporary aliases are valid for the current session and will be flushed after the session or system is restarted.
To create them, you can directly specify the name you wish to use as a shortcut, with “=
” specifying the long command.
Syntax:
$ alias shortcut="Your long command"
The following command will create an alias with the name “update
” that will update and upgrade your Debian-based system.
$ alias update="sudo apt update && sudo apt upgrade -y"
Output:
Once the alias is created, you can use the “update
” keyword to update your system instead of specifying the long command.
However, this alias is temporary and should be deleted once the current session or system is restarted. To make them permanent, add them to your shell configuration file.
Creating Permanent Aliases in Linux
The procedure of creating a permanent alias is identical to that of a temporary alias, except that it requires adding the alias to the shell configuration file.
There are two types of shell configuration files in major Linux distributions that look for aliases.
- The global shell configuration files are located at “
/etc/environment
” and “/etc/profile
“. If you specify the aliases in these files, the changes are accessible to all users. - Specific user configuration files are located at “
~/.bashrc
” for Bash, “~/.zshrc
” for Zsh, and “~/. config/fish/config.fish
” for Fish. If you specify the aliases in these files, the changes are accessible only to the currently logged-in user.
For example, if you want to use aliases for a specific user, find their default shell using the following command:
$ echo $SHELL
#OR
$ echo $0
Output:
In this case, Bash is the default shell, so edit the bash configuration “~/.bashrc
” file using your choice of text editor, like Vim or Nano.
$ vim ~/.bashrc
#OR
$ nano ~/.bashrc
And your list of aliases at the end of the file.
#Custom Aliases
alias jakepc="ssh [email protected]"
alias html="cd /var/www/html"
alias update="sudo apt update && sudo apt upgrade -y"
Output:
Save and close the file, and load the new aliases in your current shell environment using the source command.
$ source ~/.bashrc
Removing the Temporary and Permanent Aliases
You can use the unalias command with an alias shortcut name to remove the temporary aliases.
$ unalias update
If you want to remove all the aliases from your system temporarily, then use the “-a
” flag.
$ unalias -a
If you created a permanent alias by editing the shell configuration file, then following this method will undo the changes after the session or system is restarted.
To remove them permanently, simply edit the shell configuration file, remove the alias line, and source the shell configuration file to apply the changes.
That’s all you need to know about the alias command.
If you have a question related to the topic, feel free to ask it in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.