In the environment GNU/Linux It often happens that you find yourself typing the same commands over and over again. It is true that by pressing the arrow keys in the terminal window for example, you can retrieve previously typed commands. The alias Linuxhowever, are particularly useful for representing individual commands or sequences using easy-to-use abbreviations.
Thanks to aliases, users can significantly simplify the use of the command line. For example, you can reduce the number of characters you type to access a frequently used command or create custom shortcuts.
What Linux aliases are and how they work
As mentioned at the beginning, Linux aliases are “shortcuts” that represent a command or a set of them: the latter can be executed with or without customized options.
Suppose we use distribution Ubuntu: Opening the terminal window and typing alias
, you get the list of aliases set for the user currently in use. For example, the alias ll
activate the command ls -alF
. The latter, which shows the contents of the folder by displaying all files, including hidden ones, in detailed form, is certainly more complex than a simple ll
.
In fact, it is possible to create an alias using arbitrary names: even a single character is enough to refer to a specific command. Furthermore, it is possible create other Linux temporary or permanent.
Creating a temporary Linux alias
To create a other times temporarywhich will last only for the current session of the open terminal window, you can use the following syntax:
alias nome_alias="comando"
Example:
alias lla="ls -la"
In this example, lla
becomes an alias for ls -la
command used to list files in detailed format.
Creating a permanent alias
To make an alias permanent, i.e. usable at any time, even after restarting the machine or opening a new terminal window, you need to add it to the configuration file from the shell.
Bash, Zsh and Fish are all shell Unix, which are command line interfaces that allow users to interact with the operating system. Each has its specific characteristics and advantages.
Depending on the shell you are using, the configuration files are different:
- Bash:
~/.bashrc
- ZSH:
~/.zshrc
- Fish:
~/.config/fish/config.fish
To set a permanent alias just open the shell configuration file with the nano utility (example: nano ~/.bashrc
). You can then add the alias you want to use, to the end of the file (so, for example, alias lla="ls -la"
seen previously). To get the tilde character (~
) with the Europen keyboard, just hold down the key ALT Gr
while pressing ì
.
You must then save the file (CTRL+O
) and exit the text editor by pressing CTRL+X
.
Reload the configuration file allows you to apply changes without having to restart the terminal:
source ~/.bashrc
By typing, in this case, lla
you get the result corresponding to the command defined as alias. Also, writing alias
and by pressing Enter, you can ensure that the permanent alias has been added.
How to remove an alias
To delete a previously added alias, you can use the command unalias
followed by the name of the Linux alias to remove. Example:
unalias lla
Examples of useful aliases in the Linux environment
Once you have learned what aliases are and what they can be used for, you can have fun creating useful ones for everyday work. The following is an alias that allows you to update the system operational:
alias update="sudo apt update && sudo apt upgrade"
With the next one, you can quickly take yourself into the directory home:
alias home="cd ~"
View processes in detail:
alias psu='ps aux | less'
Show how the disk is used:
alias du='du -h --max-depth=1'
In general, it should be remembered that aliases cannot capture arguments like normal scripts or shell-usable functions. You can use quite complex commands and chaining within Linux aliases. To carry out even more complex activities, however, it is always good to use traditional scripts .sh
.