What is an alias in Linux ?

Alias in Linux

You will be learned about alias in Linux from this article. In the previous article we learned few very basic commands like pwd, ls , cd, vim, touch, cat, less and more. We could learn there are options we can use with the commands like options used with ls command in the previous article.

So, normally we have to use these Linux custom commands frequently. Typing the same command over and over reduces the productivity. Because of that reason we use aliases. What alias do ? It’s like a custom shortcut to represent a command or a set of commands.

Eg : Without typing ls -lrt every time, we  can create an alias in one or two characters to represent that command.

1) To view current aliases that have created. 

$ alias

What is an alias in Linux operating system ? How to make a permanent alias ?

2) How to create an alias ?

Below is the syntax,

alias <shourtcut name> =”Your custom command”

eg :
alias t=”ls -lrt” ( We can use “t” to list files sorted by time )
alias h=”ls -lh” ( We can use “h” to list files sorted by file size )
alias sshx=”ssh [email protected]” ( We can use sshx for connecting to serverx with ssh )

* But, above aliases are not permanent. They are active within that user login time. If the user signed out, those aliases will be erased and only permanent aliases will be shown. 

3) How to create a alias permanent ?

There is a file named “bashrc” ( a hidden file ) in your home directory. It will be appear as
~/.bashrc

bashrc file is a shell configuration files. This file will be run in each time of a shell interaction session.

4) What is a shell ?

Shell is a program given to users for interaction with the system. Simply an interpreter that executes commands reading from standard input keyboard or from a file. There are different types of shells in Linux.

  • Bash shell
  • C shell
  • korn shell
You can find your running shell with this command
env | grep SHELL
 
So, in order to make an alias permanent we need to put them in bashrc file in user home directory.
vi ~/.bashrc ( Open the bashrc file for editing )
alias ll=”ls -l” ( write the alias inside of the file and save it )
Logout from your account and login back. Check the alias, now your alias is permanent for your account.
* In order to make an alias permanent for all the users in your linux OS, you must create a file in /etc/profile.d directory with a name for your alias. Then edit that file and write the normal alias that we wrote in the above. After that logout from the account and log in back. Check, your alias will be in all the accounts. But remember /etc/profile.d is directory with root permissions ( simply only root can edit those files ). So to change that you either need to be the root or you should be a super user.
Useful resources :
Cheers!

Leave a Reply