Aliases are very useful when it comes to fast working in Linux. We can use alias to run a long or hard remember command with a simple word. There are two types of aliases – temporary and permanent. In this tutorial we are going to cover both topics. All commands have been executed on Ubuntu 16.04 and should work on CentOS/RHEL as well, but they are also compatible for other Linux distros.
Temporary aliases are very easy to create and remove, but they will be lost after terminal close/system reboot.
To view the list of all aliases, we can type:
Create Temporary Aliases
To create a temporary alias, we need to type the following command in the terminal:
Creating temporary alias is very helpful if we are going to run a command after doing changes on the system. For example, we can define alias to check the storage usage of the disk and type alias every time we need to run the desired command. In this particular case we must add the following alias:
alias disk=’df -h’
Afterward, to check disk space we can run "disk" and will see output like this:
Filesystem Size Used Avail Use% Mounted on udev 960M 0 960M 0% /dev tmpfs 198M 6.3M 191M 4% /run /dev/sda1 8.8G 4.9G 3.5G 59% / tmpfs 986M 212K 986M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 986M 0 986M 0% /sys/fs/cgroup tmpfs 198M 56K 198M 1% /run/user/1000
Besides of creating temporary aliases we can define permanent aliases which will be usable whenever you want and won’t be lost even after system reboot. Here are some basic alias examples that are predefined in most of Linux distributions:
alias rm=’rm -i’ alias ll=’ls -alF’ alias la=’ls -A’
Note that alias command applies only to current session.
Create Permanent aliases
To define a permanent alias we must add it in ~/.bashrc file. Also, we can have a separate file for all aliases (~/.bash_aliases) but to make this file to work we must append the following lines at the end of the ~/.bashrc file, using any text editor:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
Also we can use the following command to add alias without opening the ~/.bash_aliases file
echo "alias vps='ssh user@ip_address_of_the_remote_server'" >> ~/.bash_aliases
this alias can help us to connect to our vps server via a three-letter command
Here are some examples of permanent aliases that can help in daily work
alias update='sudo -- sh -c "apt update && apt upgrade"' # update Ubuntu distro alias netstat='netstat -tnlp' #set default options for netstat command alias vnstat='vnstat -i eth0' # set eth0 as an interface for vnstat alias flush_redis='redis-cli -h 127.0.0.1 FLUSHDB' # flush redis cache for wp
All created aliases will work next time we log in to via ssh or open new terminal. To apply aliases immediately we can use the following command:
In the second command the “.” acts as source command.
To remove an alias we must type:
To remove all defined aliases we must type:
Note that unalias command also applies only to current session.
To remove permanent alias we must delete the appropriate entry in the ~/.bash_aliases file.
As mentioned before we can use unalias command to remove an alias, but that applies only to current session and if we open new terminal (or logging in via ssh) our permanent aliases will be still available.
Note that we have a permanent alias (e.g. alias ping=’ping google.com’) and we add a temporary alias during your session with the same name (e.g. alias ping=’ping facebook.com’), the temporary one will have higher privileges during current session. Thus if we type ping we will actually ping to facebook.com and not google.com until we ssh login again.
Now we know how to add aliases in Linux and make them work for us. Let us know your comments.