The GNU/Linux terminal (namely, the Bash shell) copes with the tasks of automated system management. A user is good enough to know the shell commands to describe all the instructions in advance. Often, command aliases are used to simplify operations (not necessarily automated). This is what will be discussed in this article.
What is the Alias Command in Linux?
This command is a part of the Bash shell; its structure and functionality are independent from the distribution and are the same everywhere. Alias allows you to describe in a word a whole set of instructions entered in the terminal, even with the command that is already used in the system (but is not an alias). This is convenient for frequently performed operations, especially manual ones.
For example, the df command displays the size of disk partitions in blocks by default. This is not very convenient for estimating disk space. Therefore, usually, the -h flag is added to it to be displayed in a human-readable form. Entering it every time is inconvenient, so you should add a new alias for df:
alias df=”df -h”
After executing this command, the terminal will immediately perceive it without additional manipulations.
But these aliases work until the terminal is restarted, after which they must be re-entered. This problem can be solved by the .bashrc file (.zshrc if you are using the Zsh shell), which should be located in the user’s home directory. If it is not there, then it can be copied from the system directory:
cp /etc/skel/.bashrc ~
Some users create aliases to a separate ~/.bash_aliases file.
So, to commit a new alias, add it to the end of the file by manually editing or using the echo command:
echo “alias mount=\”mount | column -t\”” >> ~/.bashrc
In this example, the alias mount displays the required information in a readable table.
For the changes in the file to take effect, restart the terminal or enter:
It is worth noting that aliases have the highest priority in user’s space. Thus, you can reassign the execution of some utility (more precisely, the word that launches it), regardless of its original purpose.
To quickly view the instructions that belong to the selected alias, use the type command:
This is convenient for the case when you forgot the appointment of an alias or several of them with the same name and you need to decide which one to leave working.
To view all aliases specified in .bashrc, simply enter:
You can delete the selected alias with the command:
To remove all available:
Useful Aliases in Linux
The following aliases are very convenient replacements for various instructions that are inconvenient to remember or to enter each time. Here are the distinctions of distributions that can play a role.
Update and Cache Management
Full safe update:
|Arch-based||alias update=”sudo pacman -Syyu”|
|Debian-based||alias update=”sudo apt update && sudo apt upgrade”|
Clearing cache and removing downloaded packages for installation:
|Arch-based||alias clean=”yes | sudo pacman -Scc”|
|Debian-based||alias clean=”sudo apt autoclean && sudo apt clean”|
alias memory=”free -h”
Display five active processes with the highest memory consumption:
alias memps=”ps ax | sort -k4 -n | tail -5”
CPU name and frequency:
alias cpuname=”cat /proc/cpuinfo | grep "model name" | cut -f 2 -d ":" | head -1”
Rebooting the router (you must correctly specify its network address and login information):
alias rebootrouter="curl -u 'login:password' 'http://192.168.0.1/setup.cgi?todo=reboot'"
The total amount of files in the current folder in a human-readable form:
alias df=”df -sh”