命令别名将为公共命令及它们的参数创建别名,以尽可能减少录入工作。
1、查看系统已存在的公共命令别名
使用 alias -p 查看系统已存在的公共命令别名 活动列表
[root@hadoop ~]# alias -p
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2、使用alias创建命令别名
[root@hadoop ~]# alias li='ls -li'
[root@hadoop ~]# li
总用量 112
33574979 -rw-------. 1 root root 1419 6月 11 13:33 anaconda-ks.cfg
51251036 -rw-r--r--. 1 root root 22255 7月 8 13:53 customer.java
19751199 -rw-r--r--. 1 root root 20640 7月 8 13:53 date_dim.java
34855665 -rw-r--r--. 1 root root 0 7月 8 20:38 derby.log
51251011 -rw-r--r--. 1 root root 14215 7月 7 14:55 product.java
34318652 -rw-r--r--. 1 root root 11476 6月 14 10:26 QueryResult.java
51253816 -rw-r--r--. 1 root root 22853 7月 8 13:56 sales_order.java
51265292 drwxr-xr-x. 3 root root 42 6月 15 17:05 ${system:java.io.tmpdir}
33696874 -rw-------. 1 root root 1675 6月 11 16:40 y
33696875 -rw-r--r--. 1 root root 393 6月 11 16:40 y.pub
可以看到,定义了命令别名之后,可以使用别名。
注意:命令别名与本地环境变量相似,它们只对于定义范围内的shell进程有效:
[root@hadoop ~]# alias li='ls -li'
[root@hadoop ~]# bash
[root@hadoop ~]# li
bash: li: 未找到命令
3、如何使命令别名全局有效
在启动新的交互式shell时,bash shell 始终会读取
H
O
M
E
/
.
b
a
s
h
r
c
启
动
文
件
,
可
以
将
别
名
放
入
HOME/.bashrc启动文件,可以将别名放入
HOME/.bashrc启动文件,可以将别名放入HOME/.bashrc
[root@hadoop ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias li='ls -li'
[root@hadoop ~]# bash
[root@hadoop ~]# li
总用量 112
33574979 -rw-------. 1 root root 1419 6月 11 13:33 anaconda-ks.cfg
51251036 -rw-r--r--. 1 root root 22255 7月 8 13:53 customer.java
19751199 -rw-r--r--. 1 root root 20640 7月 8 13:53 date_dim.java
34855665 -rw-r--r--. 1 root root 0 7月 8 20:38 derby.log
51251011 -rw-r--r--. 1 root root 14215 7月 7 14:55 product.java
34318652 -rw-r--r--. 1 root root 11476 6月 14 10:26 QueryResult.java
51253816 -rw-r--r--. 1 root root 22853 7月 8 13:56 sales_order.java
51265292 drwxr-xr-x. 3 root root 42 6月 15 17:05 ${system:java.io.tmpdir}
33696874 -rw-------. 1 root root 1675 6月 11 16:40 y
33696875 -rw-r--r--. 1 root root 393 6月 11 16:40 y.pub