How to Create Aliases & Use the Alias Command in Linux

alias [alias_name]="[command]" on a blue gradient background

In this article, you will be acquainted with different methods to manage aliases by inspecting the alias tool to override commands, along with the common ways to set/unset temporary and persistent aliases.

What are Aliases in Linux

Linux is always associated with repeated patterns of typing complicated and long command lines. Often times Linux users make mistakes while manually executing commands (we are only humans).

This problem could be very frustrating for some. Fortunately Linux offers the possibility to minimize time and effort when executing long commands by using Aliases.

The alias command is considered by far one of the most useful utilities in Linux; it’s a method that creates a shorthand for long and complicated command lines, essentially overriding an existing command with a brand new command by the user’s choosing.

Most Linux users make use of this method to boost efficiency by reducing interaction with their keyboards while dealing with day to day command lines.

How to Set an Alias

Syntax to define an alias:

alias [alias_name]="[command]"

As you guessed it, an alias can be defined by using the alias command. Let’s start by choosing the command line that it will be overridden by our new alias.

Suppose that we have to echo the next line every time we boot our machine: Hello Mr. ZzIyxbAzIIING, please call the mother ship. This could be frustrating, unless we aliased it as so:

alias Hi="echo Hello Mr. ZzIyxbAzIIING, please call the mother ship"

Now let run the “Hi” command:

Hi
Output
Hello Mr. ZzIyxbAzIIING, please call the mother ship

Note, we can also create aliases in these files: .bash_aliases / .bashrc; these types of defined aliases are called persistent aliases.

Let’s try the alias command for more practical examples: To get a full list of hidden files and their info, we use this command ls -la, but for our case we want to achieve similar results by executing a:

alias a="ls -la"
a
Output
total 124
--info

Note: alias names cannot contain the following characters: /, $, '', =

To print out the list of aliases that we’ve created, run the mentioned below command:

alias -p
Output
alias Hi='echo Hello Mr. ZzIyxbAzIIING, please call the mother ship'
alias a='ls -la'
--other aliases

Note that we can achieve similar results with running alias without the –p option

As you may have gathered, other aliases that you did not define, comes by default when starting the terminal.

In case you have a long list of aliases and you wanted to examine a specific alias that you’ve created, just feed the alias name to the alias command:

alias a
alias a='ls -la'

How to Unset an Alias

How about removing an alias? Simply execute the unalias command followed by the alias name:

unalias Hi
alias Hi
Output
bash: alias: Hi: not found

If you choose to delete all existing aliases run the mentioned below command:

unalias -a
alias -p

–Notice that now the alias -p returns nothing, even the default defined aliases.

How to Override an Alias

Suppose we defined the alias below:

alias ls="ls -la"

Now every time we run ls command, it will return the hidden files and their info located in the current directory.

How about for some reason we didn’t want to use the original command ls? In other words, we want to override the alias name that we’ve just created. To do that, start with a backslash \ followed by the command that has a similar alias name just as so:

\ls
Output
bash_article  Downloads     mydiscord.deb  Public     Videos
Desktop       grep_testing  myscript.sh    snap
Documents     Music	    Pictures	   Templates

Note that you can also achieve similar results by running these variations:

"ls" #or# 'ls' #or# command ls

How to Set Persistent Aliases

As you might have noticed that after starting a new terminal window you won’t be able to run your aliases. So the method of creating aliases by executing the alias command has a very short life, which means that everything you setup by executing the alias command is session specific. The next question we should ask is: how can we make these aliases permanent? To achieve this, we can simply append whatever alias command to our ~/bashrc or ~/.bash_aliases files.

~/.bashrc

Start by opening the bashrc file using your favorite text editor:

nano ~/.bashrc

Scroll all the way down to the bottom, you can add a comment to make things organized, then copy and paste that ls alias the we’ve used in the previous example just like the following:

My permanent aliases
alias ls="ls -la"

Save and exit the .bashrc file, then source it for the changes to take effect in the current session:

source ~/.bashrc

~/.bash_aliases

We can be more organized and achieve similar effect by adding aliases to the ~/.bash_aliases file which is sourced from the .bashrc file. Start by creating the file:

nano ~/.bash_aliases

Add the ls alias that we’ve used in the previous example:

My ls aliases
alias ls="ls -la"

After saving the file; just like before, source it for the changes to take effect in the current session.

. ~/.bash_aliases

Note that the ~/.bash_aliases file work only if the .bashrc file contains the mentioned code below:

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

To define new aliases in your ~/.bash_aliases a lot faster, you can use the echo or the cat command with the output >> operator to append new aliases to the existing file.

echo alias rm="rm -v -i" >> ~/.bash_aliases
cat ~/.bash_aliases
Output
alias ls="ls -la"
alias rm=rm -v -i

To add an alias using the cat method, run the following commands:

cat >> ~/.bash_aliases
Output
alias sl='ls --human-readable --size -1 -S --classify'
^C

After adding all your favorite aliases to either bashrc or bash_aliases file located on your home directory, your account will have access to all added aliases every time you run the terminal.

Taking Advantage of Aliases

Aliases are an awesome concept, to take a specific command and manipulate it by naming and shortening that command. Linux users could really be creative when dealing with bash aliases. Check the following command aliases that could make the life of a Linux enthusiast a lot easier.

Easy navigation (Alias of an alias):

alias ..='cd ..'
alias ...='cd ../..'
alias .3='..;...'

Delete files in an interactive and verbose manner:

alias rm="rm -v -i"

Update all packages:

alias updg="sudo apt update ; sudo apt upgrade -y"

Install packages:

alias i="sudo apt install"

Clear the screen:

alias f="clear"

List the current directory content by size and format:

alias sl='ls --human-readable --size -1 -S --classify'

Source the bashrc file:

alias src='source ~/.bashrc'

Conclusion

In this article, we’ve learned about the Alias command that alleviates the difficulties of the repeating patterns of command lines. We also discussed the different ways of setting up aliases by command line (temporary) and by appending the alias command to the bashrc/bash_aliases file (permanent).

Additionally, we talked about how to remove a specific alias or all aliases by command line, not to mention overriding an alias which is a neat trick to ignore that alias.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Linux Runlevels Explained
Read More

Linux Runlevels Explained

There are times when Linux system boots either into a Graphical User Interface (GUI) or a command line.…