In Linux, an alias is used as a command that is a shortcut for another command. Aliases are often used to type a command without typing the extra options that are needed. This can save time by needing to type less for commands that are used often and reduce time referencing documentation for a command. This article will cover how to view aliases that are currently available and how to create your own aliases.
How To View Aliases
To find out what aliases are currently available just type alias at the terminal. The below screenshot shows the default aliases provided by the CentOS Linux distribution.
The above screenshot shows the list of aliases that already exist for the user session. Typing the command on the left side of the equals sign will run the command on the right side of the equals sign. It is common for the –color=auto option to be added to commands using aliases. For example, anytime the ls command is ran it will run ls –color=auto to provide color in the output. The –color=auto is an option that improves the output of the command, but it can be tedious to type every time using the ls command so the alias is a helpful shortcut.
The below screenshot shows the output of running the alias command when logged in as root user to view the available aliases. This shows that by default root has most of the same aliases as a regular user and also for the cp, mv, and rm commands the -i option is used to prompt before each action.
How To Add An Alias To The Current Session
To add an alias to the current shell session, use the alias command with the following format:
As an example alias to create, to edit the Apache HTTP server config file in vim it can be common to type the following command or a similar command:
It can be inconvenient to type out the path to the config file and time consuming to search for the file when you forget where it is located. We can create an alias to run this command and name the alias eac to be short for edit Apache config using the the command:
alias eac='vim /etc/httpd/conf/httpd.conf'
The below screenshot shows creating this alias at the terminal and then running the alias command to confirm that it has been added to the list of aliases. After adding this alias, typing eac at the terminal will open the vim editor and open the /etc/httpd/conf/httpd.conf file in vim.
It is important to note that using the alias command on the terminal to create an alias will only create an alias that is available during the current shell session. When you log out of the shell session any aliases created on the terminal will be gone the next time you login.
How To Create A Permanent Alias
As stated in the previous section, using the alias command on the terminal to create a new alias will only create an alias that is available for the current shell session. To create an alias that will be available anytime after logging out and logging back in, then the .bashrc file will need to be updated.
Each user has a .bashrc file located in their home directory so make sure to edit the .bashrc in the home directory of your user account. If you are unsure how to open your user’s accounts .bashrc file then you can open ~/.bashrc in your preferred editor like the following examples:
It is common for the .bashrc file to have a section for aliases or the alias can be added to the bottom of the file. The below screenshot shows an alias being added to the root user’s .bashrc file.
After adding an alias to the .bashrc file and saving the file the new alias will not be available to the current session. This is because the .bashrc file needs to be ran with the updates. When you log off of the session and log back into a new session the new alias will be available to use. The new alias will remain available for any new session unless you decide to remove it from the .bashrc file.
How To Override An Alias
If a Linux command has an alias that you do not want to use at the moment then add a backslash (\) in front of the command.
For example, the first section for viewing aliases for a root user shows the rm command has an alias that uses rm -i to use the interactive switch to prompt for a confirmation before deleting each file. To not use the alias with the -i switch the command can be ran as \rm files.
Aliases are shortcuts to other commands to save time. The alias command can be used on the terminal to view current aliases and to create an alias for the currently logged on shell session. To create a permanent alias for the user, the alias will need to be added to the .bashrc file.