Command alias in Linux, Unix

  • 时间: 2017-06-15 03:33:20

Command alias

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 inLinuxis 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, east and accurate to work in shell. On other hand, if you want users to use some commands with preferred switch always, you can create alias of it. For example, rm command can be aliased to ‘rm -i’ so that it will be always interactive asking user to confirm his remove operation.

In this article we will see how to create command alias, how to remove alias and how to list alias.

How to create alias

Creating alias is 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 time stamp. Saving out time to write long command with arguments. See how differently ls works before and after alias in below output.

 # lsapache  httpd-2.4.25  httpd-2.4.25.tar  letsencrypt  lolcat-master  master.zip  shti # alias ls='ls -lrt' # lstotal 38504-rw-r--r--.  1 root root  39198720 Dec 19 12:29 httpd-2.4.25.tardrwxr-xr-x.  5 root root      4096 Dec 26 07:48 lolcat-master-rw-r--r--.  1 root root    205876 Mar 22 02:21 master.zipdrwxr-xr-x.  2 root root      4096 Mar 29 08:15 apachedrwxr-xr-x. 12  501 games     4096 Mar 29 08:42 httpd-2.4.25drwxr-xr-x. 14 root root      4096 Apr  3 15:07 letsencryptdrwxr-xr-x.  2 root root      4096 May 16 01:29 shti 

Make a note that this alias will be available in 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 individual user. Defining it in profiles is same syntax, just add above command in 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.

 # aliasalias 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 above output you can see all commands on left of = and values they are aliased for in right section. These aliases are defined on /etc/profile or user profiles or shell profiles or defined in 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 above listing) as argument.

 # unalias ls # aliasalias 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’ is vanished from alias listing. Keep in mind that this un-alias is limited to current shell only. If you want to permanently un-alias command then you need to remove it from 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 terminal. You can alias funny statements to commonly used commands. For example aliasing ls tocmatrix command to run matrix green falling code in terminal and stun user!

 # alias ls=' echo I love kerneltalks.com :D' # lsI love kerneltalks.com :D 

As you know how it can turn evil if gets into wrong hands! So be extra cautious when defining alias for destructive commands.