In Linux, aliases are shortcuts that replace long or frequently used commands with shorter, more convenient ones, improving efficiency by speeding up the workflow and avoiding potential spelling errors. They can also replace commands with additional options, making them easier to use.
Alias Benefits
Save time by typing less.
Improve command memorability through custom names.
Increase efficiency by automating repetitive tasks.
Linux Alias Syntax
To create an alias, use the alias
command and its syntax as follows:
alias [name]='[value]'
alias
: Invokes thealias
command.[name]
: the name of the alias is being created. This is what will be typed in the terminal instead of the full command. A name is a user-defined string, excluding special characters and 'alias', 'unalias' keywords that cannot be used as names.[value]
: the actual command that the alias represents. This can be a simple command or a more complex command chain. Commands can also include options, arguments, and variables.
Create Aliases in Linux
There are two types of aliases to create in Linux:
Temporary. Add them using the
alias
command.Permanent. These require editing the system files.
Create a Temporary Alias
Aliases defined directly in the terminal are temporary and last only for the current terminal session. Use the alias
command to create a temporary alias. For example, adding move
as an alias for the mv
command with the option of asking for confirmation before overwriting:
alias move='mv -i'
Another use for aliases is to create a shortcut for running scripts. To do this, provide the absolute path to the script as the value:
alias frename='Example/Test/file_rename.sh'
In this example, using frename
as a command runs the file_rename.sh
bash script.
Create a Permanent Alias
To make an alias permanent, add it to the shell's configuration file, such as ~/.bashrc
or ~/.bash_profile
, and then source the file or restart your terminal. Depending on the type of shell you are using, use:
Bash shell: ~/.bashrc
Zsh shell: ~/.zshrc
Fish shell: ~/.config/fish/config.fish
Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and vim text editor:
sudo vim ~/.bashrc
Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the alias
command syntax.
In our example:
#Custom aliases
alias c='clear';
alias df='df -h -x squashfs -x tmpfs -x devtmpfs'
Once all of the new aliases are added, press ESC, type :wq! to save the changes to the configuration file.
The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the source
command:
source ~/.bashrc
The output of the df
command before creating an alias for it:
The output of the df
command after creating an alias for it:
List Aliases in Linux
Using the alias
command on its own displays a list of all currently set aliases:
alias
Remove Aliases in Linux
To remove an alias, use the unalias
command with the following syntax:
unalias [name]
Adding the -a
option allows to remove all aliases:
unalias -a