Linux alias command

Filed Under: UNIX/Linux

As a Linux user, you will tend to use the same commands time and time again. This will often tend to negatively impact your productivity. To save yourself from this inconvenience , alias command comes in handy. An alias is a custom shortcut that represents another command. It’s a shell command that allows you to define your own command based on a predefined set of pre-existing commands.

To put this into perspective, let’s have a look at a few alias command examples.

Listing current aliases in the system

Your Linux system comes with predefined aliases. To view pre-existing aliases, run

$ alias

OR

$ alias -p

Output

Alias Command

To verify that the aliases are indeed working, we will take one example. We will run the ls command and compare the output with ls --color=auto.

$ ls

OR

$ls --color=auto

Output

alias command

As we can see from the output above, both commands yield the same result. Executing

$ ls

is the same as executing

$ ls --color=auto

Creating aliases in Linux

Let’s now learn how to create aliases in Linux. The process is quick and relatively easy. There are two types of aliases,

  1. Temporary aliases
  2. Permanent aliases

Let’s take a look at both

Temporary aliases

To create an alias use the syntax as shown below

$ alias command='command shortcut'

For instance, I’m going to create an alias to pinging Google’ DNS ping 8.8.8.8 -c 4 .

$ alias net='ping 8.8.8.8 -c 4'

Output

Set Temporary Alias

To confirm that the alias works run

$ net

Output

Run The Alias Command

Great! The alias is working. However, this will not persist after a reboot. Therefore, we need to make this alias permanent.

Permanent aliases

To enforce and make the alias permanent, you need to add it to the ~/.bashrc file

$ vim ~/.bashrc

Next, append the alias at the end of the file.

Output

Add Alias To Bashrc file

Save and exit the text editor. To confirm if the alias is persistent, reboot your PC

Now check the aliases existent in your system

$ alias

How to remove an alias

To remove an alias use the syntax below

$ unalias alias_name

In our example, the command will be

$ unalias net

If you check again with the alias command, the alias will not be included in the list of aliases

Unalias Command

Wrapping up

That was a brief tutorial on the alias command. Your feedback is most welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages