Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

The alias command is a handy tool to have if you often work on the command line. If set correctly it will help you to type your commands faster as well as it will even help you to correct your typos. alias is a shell builtin command, which means that no installation is required as it is available on your system by default. This tutorial will teach you how to use the alias command in the Linux operating system by explaining the alias command in more detail with use of examples.

2. What is the alias command in Linux?

alias is used to create alternative versions of your regular commands by giving them new names. It is also used to create new simpler to remember commands from obscure and long commands. With alias you can also set default options to be used every time you execute the alias command.

3. How to create alias in Linux

How many times it happened to you that instead of ls command you have typed sl? And end up with error:

$ sl
-bash: sl: command not found

Let's use the above, to show you an example on how to create alias in Linux and to avoid the above error message. To create alias use alias command with the following syntax:

alias alias_command_name='commands'

Therefore, we can create a new alias called "sl", which will be an alternative command to ls command.

 # ls
./  ../  alias.txt
$ sl
-bash: sl: command not found
$ alias sl=ls
$ sl
./  ../  alias.txt

From the above you can see that we have created a new alias "sl", which when executed runs ls command. This was just a basic example of how to create a new alias. With use of single quotes you can use alias to include options and multiple commands. For example, let us create an alias which will display our external IP address:

$ alias myip='wget -qO myip http://www.ipchicken.com/;
grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" myip;  rm myip'

Execute the above command on a single line to create a new alias called myip, which will display your external IP:

$ myip
67.64.74.223

It is important to note that when using the alias command you create alias for a current shell session only. This means that once you log out or open new shell your newly created aliases would not be available. The next section will explain how to make an alias permanent on your system.

4. Making alias permanent

Normally you will define all your aliases to be permanent. One way to do this is to set your alias by including it to you local ~/.bashrc file. Here we assume that your default shell is bash.

$ ls
alias.txt
$ echo 'alias ls="ls -l"' >> ~/.bashrc
$ . ~/.bashrc
$ ls
total 0
-rw-rw-r-- 1 lubos lubos 0 Jan 23 13:06 alias.txt

 Let's go line by line to explain what happened in the alias example above:

  1. We have executed the ls command without long listing -l option. This is the default behavior of the ls command.
  2. Next, we have inserted new ls alias with -l option into ~/.bashrc file. .bashrc file is a hidden file and it is located in your home directory. Instead of the echo command you can use some text editor. It is important to note that if you decide to use the echo command, as in the above example, make sure that you first make a backup copy of your ~/.bashrc file and that you use append operator >> instead of >.
  3. With a "." operator we have sourced the ~/.bashrc file which means that bash environment was updated to include our new alias. After reboot or when creating a new shell login this is done automatically thus now our alias is permanent.
  4. Lastly, we have executed the aliased ls command, which now includes -l option

5. Display alias list

 To show all currently defined aliases in your shell simply execute the alias command without any arguments. ( on some systems use -p option ):

$ alias -p
alias attrib='chmod'
alias chdir='cd'
alias copy='cp'
alias d='dir'
alias del='rm'
alias deltree='rm -r' .....

6. Removing alias

If you no longer need alias you can remove it from the system by using the unalias command. Simply, execute the unalias command along with alias name you wish to remove as an argument.

$ alias | grep ls
alias ls='ls -l'
$ unalias ls
$ alias | grep ls
$

If you have defined your alias within ~./bashrc file you will also need to remove this alias definition from there. Otherwise the alias will be defined again once you start a new shell or reboot your PC.

7. Conclusion

alias is handy tool in your pocket as it helps you to work more efficiently on Linux's command line. It is simple to use and configure. Most of the Linux systems come with predefined aliases. Feel free to check what are those and alter them or add new to fit your needs. If you cannot decide what alias to add, the good candidate for an alias will be a long and boring command you use regularly.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.