首页 > 技术文章 > 系统别名概念(alias)

xgg123 2022-01-08 12:20 原文

 [root@hgg ~]# ls -l /etc/hosts
 -rw-r--r--. 1 root root 180 Jun 29 16:21 /etc/hosts
 [root@hgg ~]# ll /etc/hosts
 -rw-r--r--. 1 root root 180 Jun 29 16:21 /etc/hosts

说明:ll命令就是ls -l命令的别名

作用:01、别名可以使命令操作更加简单

02、让危险的命令操作更简单

查看系统设置的别名:

 [root@hgg ~]# 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 rm='rm -i'
 alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

 

需求01:如何让一个命令操作简单(设置别名)

1)临时别名设置:

alias 别名名称='命令信息'

alias catnet='cat /etc/sysconfig/network-scripts/ifcfg-eth0'

 
 [root@hgg ~]# alias catnet='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
 
 [root@hgg ~]# alias
 alias catnet='cat /etc/sysconfig/network-scripts/ifcfg-eth0'

需求02:rm 命令执行 == echo "rm command is weixian,not use"

 
 [root@hgg ~]# alias rm='echo rm command is weixian,not'
 
 [root@hgg ~]# rm -rf /hgg
 rm command is weixian,not -rf /hgg

2)使别名失效

01、重新修改回来

02、取消别名

unalisas rm

03、利用"\"(撬棍)

\rm -rf /hgg

04、绝对路径方式执行

/usr/bin/rm -rf /hgg

补充:系统中将命令分为两大类(type)

外置命令: 需要进行安装

内置命令:所有系统都内置的命令

查看是否是外置或者外置命令方法:

 
 
 [root@hgg ~]# type cd
 cd is a shell builtin--内置命令
 [root@hgg ~]# type mkdir
 mkdir is /usr/bin/mkdir--外置命令
 
 

 

3)永久别名设置

如何设置永久别名 rm别名

方法一:使用命令:bashrc/prcfile

[root@hgg ~]# vi /etc/bashrc

[root@hgg ~]# vi /etc/profile

将别名命令放到文件最后

alias rm='echo rm command is weixian,not'

方法二:vi /root/.bashrc 相对方法一更优先

注释此行:#alias rm='rm -i'

 

vi /root/.bash_profile

将别名命令放到文件最后

alias rm='echo rm command is weixian,not'

推荐阅读