Learn how to create, view, and remove command alias in Linux or Unix. Useful to shorten long commands or imposing switches to commands.
Command alias in Linux is another (mostly short) version of frequently used command (or command with arguments). It is meant for lessening keystrokes to type long commands and making it fast, easy, and accurate to work in shell. On the other hand, if you want users to use some commands with preferred switch always, you can create an alias of it. For example, rm command can be aliased to rm -i
so that it will be always interactive asking the user to confirm his remove operation.
In this article we will see how to create command alias, how to remove the alias, and how to list alias.
How to create alias
Creating alias is an easy job. You have to specify alias and command to alias like below :
# alias ls='ls -lrt'
Here in this example, we are aliasing command ls
to ls -lrt
so that whenever we type ls command it will long list, reverse order with a timestamp. Saving out time to write long command with arguments. See how differently ls works before and after alias in the below output.
# ls
apache httpd-2.4.25 httpd-2.4.25.tar letsencrypt lolcat-master master.zip shti
# alias ls='ls -lrt'
# ls
total 38504
-rw-r--r--. 1 root root 39198720 Dec 19 12:29 httpd-2.4.25.tar
drwxr-xr-x. 5 root root 4096 Dec 26 07:48 lolcat-master
-rw-r--r--. 1 root root 205876 Mar 22 02:21 master.zip
drwxr-xr-x. 2 root root 4096 Mar 29 08:15 apache
drwxr-xr-x. 12 501 games 4096 Mar 29 08:42 httpd-2.4.25
drwxr-xr-x. 14 root root 4096 Apr 3 15:07 letsencrypt
drwxr-xr-x. 2 root root 4096 May 16 01:29 shti
Make a note that this alias will be available in the current shell only. If you want to make it permanent over reboots, spawns over other users and shells then you need to define it in /etc/profile
or respective shell profiles of the individual users. Defining it in profiles is the same syntax, just add the above command in a profile file and it will works once the profile is sourced.
List alias in current shell
To view and list all aliases currently active in your shell, just type command alias
without any argument.
# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls -lart'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
In the above output you can see all commands on the left of =
and values they are aliased for in the right section. These aliases are defined on /etc/profile
or user profiles or shell profiles or defined in the shell with alias command.
How to delete alias
In case you want to remove or delete alias you defined, you need to use unalias
command. This command takes your alias command (left portion of =
in the above listing) as an argument.
# unalias ls
# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Observe in above output after un-aliasing ls, its alias to ls -lart
vanishes from alias listing. Keep in mind that this un-alias is limited to the current shell only. If you want to permanently un-alias command then you need to remove it from the profile file where you have defined it and re-source that profile.
Fun in terminal using alias
By now you know how alias works, you can play around to have some fun in the terminal. You can alias funny statements to commonly used commands. For example aliasing ls
to cmatrix command to run matrix green falling code in terminal and stun user!
# alias ls=' echo I love kerneltalks.com :D'
# ls
I love kerneltalks.com :D
As you know how it can turn evil if it gets into the wrong hands! So be extra cautious when defining an alias for destructive commands.
Share Your Comments & Feedback: