Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

It’s pretty common for Linux users to use the same commands repeatedly over time. Copy-pasting commands through terminal history is a tedious and time-consuming task that subsequently reduces user productivity. It’s always a good idea for Linux users to optimize their workflow and maximize work efficiency.

For instance, we can quickly create shortcuts for complex commands and reuse them umpteen times instead of typing or remembering them.

Aliases help us to solve this problem easily, and they’re akin to Windows shortcuts.

Now, let’s get into the nitty-gritty of it.

2. Create Aliases

Generally, an alias is a custom command defined by the user to execute a set of complex commands:

alias shortcut-name = commands-to-execute

[email protected]:~/baeldung$ du -hs /var/log/* | sort -hr | head -3 
4.1G    /var/log/journal 
239M    /var/log/btmp.1 
48M     /var/log/logstash 
[email protected]:~/baeldung$ alias top3logs='du -hs /var/log/* | sort -hr | head -10' 
[email protected]:~/baeldung$ top3logs 
4.1G    /var/log/journal 
239M    /var/log/btmp.1 
48M     /var/log/logstash 

Also, creating an alias is a relatively easy and effective process. There are two types of alias: permanent and temporary.

2.1. Temporary Alias

If an alias is active only as long as our session is active, we categorize it as temporary. These aliases have no effect when we open a new session or terminate the current session.

Let’s see a quick illustration:

[email protected]:~$ date 
Tue Aug  3 05:24:14 IST 2021 

[email protected]:~$ who -u 
tools    pts/0        2021-08-03 05:24   .         20728 (122.164.227.87) 

[email protected]: $ pwd 
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4 
[email protected]:~$ alias goto_elk_jar_path='cd /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4/' 
[email protected]:~/baeldung$ pwd 
/home/tools/baeldung 

[email protected]:~/baeldung$ goto_elk_jar_path 

[email protected]:~$ pwd 
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4 

The who -u command helps us to get the current session details.

Let’s terminate the current session (PID: 20728) and open a new session (PID: 25267), as shown below.

[email protected]:~$ date 
Tue Aug  3 05:37:04 IST 2021 

[email protected]:~$ who -u 
tools    pts/0        2021-08-03 05:36   .         25267 (122.164.227.87) 
[email protected]:~$ goto_elk_jar_path 
goto_elk_jar_path: command not found 

The “command not found” error tells us that we don’t have any custom alias command with the name of “goto_elk_jar_path”.

2.2. Permanent Alias

Usually, CLI is a very common touchpoint for programmers, and we need to keep the alias permanently for quick execution of commands. There are two prominent ways to add an alias permanently in Linux.

One option is to add the alias directly into the .bashrc file. Then, using the source command, we can execute the file and implement the alias in the current session itself:

vi ~/.bashrc 
alias apt-update='sudo apt update && apt upgrade -y' ## Add this line in the file and save the file using :wq!.
source ~/.bashrc 

Alternatively, we can also manage all aliases into a single file known as .bash_aliases:

vi ~/.bash_aliases 
alias apt-update='sudo apt update && upgrade -y' ## Add this line in the file and save the file using :wq!
source ~/.bashrc 

Be sure to call the .bash_aliases from the .bashrc file as illustrated below:

if [ -f ~/.bash_aliases ]; then 
. ~/.bash_aliases 
fi 

Now, let’s try to execute the alias created for apt-get repository updates and upgrades:

[email protected]:~$ apt-update 
Hit:1 https://artifacts.elastic.co/packages/7.x/apt stable InRelease 
.. 
.. 
.. 
Reading package lists... Done 
Building dependency tree 
Reading state information... Done 
All packages are up to date. 
Reading package lists... Done 
Building dependency tree 
Reading state information... Done 
Calculating upgrade... Done 
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 

3. List the Aliases

Here, the alias command will list all active aliases that are currently in the userspace, including both temporary and permanent aliases:

[email protected]:~$ alias | grep -E "temp|apt" 
alias apt-update='sudo apt update -y && sudo apt upgrade -y' 
alias temp_disk='du -h .' 

Likewise, the && command will ensure that only a successful execution of the first command can invoke the execution of the second command.

4. Remove an Alias

Similarly, the unalias command helps to remove both temporary and permanent aliases attached to the current user session:

[email protected]:~$ alias temp_alias_disk='du -h .' 

[email protected]:~$ alias | grep -E "apt|temp" 
alias apt-update='sudo apt update -y && sudo apt upgrade -y' 
alias temp_alias_disk='du -h .' 
[email protected]:~$ unalias apt-update temp_alias_disk 

[email protected]:~$ alias | grep -E "apt|temp" 
[email protected]:~$ 

However, when we open a new session, any permanent aliases we removed in the previous session are still active because they’re hardcoded in the .bashrc file.

We have to remove the corresponding alias lines from the .bashrc file to remove them permanently.

5. Aliases with Arguments

Many times, an alias becomes handier when we can pass arguments to it. A simple bash function helps us to perform this operation.

General Syntax:
function fn_name {
[commands]
}

Let’s define the function in the ~/.bashrc file that creates the nested directory and then takes us into it.

Usually, after adding the code in the ~/.bashrc file, we use the source command to execute the file:

function make-dir-cd() 
{ 
    mkdir -p -- "$1/$2/$3" && cd -P -- "$1/$2/$3" 
} 

Here, $1, $2, and $3 are the arguments that are passed on CLI in the corresponding positions after the function name:

[email protected]:~$ vi ~/.bashrc 
[email protected]:~$ source ~/.bashrc 
[email protected]:~$ pwd 
/home/tools 
[email protected]:~$ make-dir-cd bael dung alias 

[email protected]:~/bael/dung/alias$ pwd 
/home/tools/bael/dung/alias 

6. Conclusion

In summary, an alias is similar to a shortcut in Windows. Using the alias command, we can create custom commands to call other commands (complex in nature or scripts). Using aliases improves productivity and can make our day-to-day workflows easier.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!