Linux中的别名
别名的作用:将一些较长的命令进行简化
alias 命令用来设置别名,仅限于本次登入生效。要永久修改,修改配置文件~/.bashrc
临时修改
用法:
alias //直接回车,显示已经定义好的别名
[root@localhost ~]# alias
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 perlll='eval `perl -Mlocal::lib`'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
发现:ls,mv,rm,cp等命令都有一些别名
其实我们使用rm命令的时候,调用的其实是别名“rm -i”,加了-i选项,在删除文件的时候,会有一个确认的提醒。
如果我们不想使用别名的命令,要使用原始命令的话,要使用绝对路径来使用命令:
如:/bin/rm file1 //删除file1
创建一个别名:
alias 新的命令=‘原命令 -选项/参数’
配置举例:
alias la=‘ls -la’
[root@localhost ~]# alias la='ls -la'
[root@localhost ~]# la
total 788672
dr-xr-x---. 6 root root 4096 Dec 6 10:51 .
dr-xr-xr-x. 18 root root 4096 Dec 4 12:12 ..
-rw-r--r-- 1 root root 7 Dec 6 10:50 1
drwxr-xr-x. 2 root root 47 Nov 23 15:57 18.06.21.8
-rw-------. 1 root root 1682 Nov 23 13:10 anaconda-ks.cfg
-rw-------. 1 root root 1289 Dec 5 04:37 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
删除一个别名:
unalias 别名
配置举例:unalias la
[root@localhost ~]# unalias la
永久修改别名
编辑根目录下的.bashrc文件
vim ~/.bashrc
增加如下图的alias la=‘ls -la’ //增加la别名
配置举例
[root@localhost ~]# vim ~/.bashrc
//以下是.bashrc文件中的内容
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias la='ls -la' //增加la别名
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
让shell重新读取~/.bashrc下的文件
[root@localhost ~]# source ~/.bashrc
测试是否生效
[root@localhost ~]# la
total 788676
dr-xr-x---. 6 root root 4096 Dec 7 10:04 .
dr-xr-xr-x. 18 root root 4096 Dec 4 12:12 ..
-rw-r--r-- 1 root root 7 Dec 6 10:50 1
drwxr-xr-x. 2 root root 47 Nov 23 15:57 18.06.21.8
-rw-------. 1 root root 1682 Nov 23 13:10 anaconda-ks.cfg
-rw-------. 1 root root 1850 Dec 7 09:52 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r-- 1 root root 194 Dec 7 09:52 .bashrc