Here in this post,we will learn very useful alias command with examples.In linux and Unix like operating system,mainly we use the alias command to shorten the long command.It is something like instead of writing long command in terminal,you type a few letters on terminal and your desired command get run.

To understand the meaning of alias,I would like to simply give example from our life.For eg. a person has name very long name like Mr. Ching chong lamb chong lamba bla bla,rather then addressing him we have his alias name called Mr. bla . So here rather than speaking a very long name of person,we are using his alias name that is Mr. bla .

Similarly,if you have very long command like vi /etc/apache2/conf.d/example-file.conf, instead of typing long command.We can make its alias command like viapache. Then only hitting the command viapache, it will open the file /etc/apache2/conf.d/example-file.conf in vi editor .

How to set alias

To set the alias of a command,we have to use following syntax in terminal.

Note: If you run alias command directly into terminal.Once you logout or system get restarted,the alias of a command will be unset itself. To set alias permanently we have to edit ~/.bashrc , /etc/profile,~/.bash_profile depends upon the user requirement.

alias name=[value]

Example 1: To clear the terminal screen

alias cls="clear" 

Example 2: To list directory(including hidden)

alias ldir="ls -la|grep ^d"

Example 3: File modified in last 30 days in /etc folder

alias f30="find /etc/ -iname "*.conf" -mtime -30 -print"

Example 4: We can use any name for alias. Even you can also set alias with your name like alias sharad="ls -lhrt"

Example 5: Combining two or more commands in alias.

alias ldf60="find /etc/ -iname "*.conf" -mtime -30 -print;ls -la|grep ^d" 

How to permanently set alias

Here we will set the alias command for a particular user and all user.

Set permanent alias for particular user

Go to user’s home directory and edit any one of the file called .bash_profile or .profile .and write your alias command in the file

Example;

cd /home/username/

vi ~/.bash_profile

alias cls="clear"

To immediately make the change available ,now run below command .(Note:When the user login back ,the changes will be effective automatically)

source /home/username/.bash_profile 

Set permanent alias for root

Edit the /root/.bashrc file and write your alias command

vi /root/.bash_profile

alias cls="clear"

To make it effective immediately.(Note:When the root user login back and after system reboot,the changes will be effective automatically)

source /root/.bashrc 

To set alias for all users in system

Edit /etc/profile file and write your alias command in it.For doing this change you must be root or the user must have superuser privelege

vi /etc/profile

alias cls="clear" 

Save the file and runt the source command for immediately applying the changes effect

source /etc/profile 

How to unset or remove alias

To remove the particular alias from the system use the below given syntax

unalias alias-name

example: unalias ldf60 (see example 5 in above paragraph)

To remove all alias with single command

unalias -a

Note: In system might have the alias has been set permanently. Check the files /etc/profile and in user’s home directory ~/.bash_profile,~/.profile and ~/.bashrc. In some system. Hence remove or comment the line related to the alias which you want to remove

Tip: You may also use alias with already available internal/external command .
for eg. In this example we have set rm command alias which is interactive. When every time user hit the command rm ,it will ask yes or no for deletion of file/directory

alias rm="rm -i" 

Sometimes,after setting above given alias, you need the removal of many files with interaction.In that case use along with alias name.Then it will be work like your previous internal/external command without interaction.
See below example

rm *.txt 

Read Some More Articles

2 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.