Linux中alias别名的设置,零时取消,永久生效

时间:2022-09-16 15:52:37

给危险的命令加上一个保险:alias别名使用相关

rm 命令是linux中相当危险的一个命令 一不下心就会酿成大错比如: rm -rf / home/test
上面的/ 和 home中间如果不小心多了一个空格那就完了
所以我们一般把rm 命令通过别名的方式优化,使用的时候只需要零时生效使用就可以

查看所有别名

[root@VM_179_129_centos oldboy]# alias
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'

这里面可以看到alias的语法格式 alias xx=” 注意等号两边没有空格,”中间的内容必须是命令

查看具体某个别名

[root@VM_179_129_centos oldboy]# alias ll
alias ll='ls -l --color=auto'

给rm 加别名

[root@VM_179_129_centos oldboy]# alias rm='echo can not use rm'
[root@VM_179_129_centos oldboy]# ls
alex.txt oldboy.txt test test.sh t.sh
[root@VM_179_129_centos oldboy]# rm t.sh
can not use rm t.sh

可以看出此时别名已经可以使用了,但是只要重新断开连接再连上别名就失效了

让别名永久生效

#修改/etc/profile 文件最后一行加入alias rm='echo do not use rm'
[root@VM_179_129_centos oldboy]# tail -2 /etc/profile
export PATH=/usr/local/git/bin:$PATH
alias rm='echo can not use rm'
#让文件配置生效
source /etc/profile

此时断开重新登录alias rm 发现不是修改后的,rm 别名还没有生效,接下来还有一步

查看/root/.bashrc 文件,发现还有rm别名设置,我们要做的就是注释掉它
Linux中alias别名的设置,零时取消,永久生效

#alias rm='rm -i'

断开,重新链接 测试rm 命令被成功永久性别名了
那我们要想删文件怎么办?我们可以让他零时生效使用

临时使用rm

使用\rm 或者 rm的绝对路径

[root@VM_179_129_centos oldboy]# ls
alex.txt oldboy.txt test test.sh t.sh
[root@VM_179_129_centos oldboy]# rm t.sh
can not use rm t.sh
[root@VM_179_129_centos oldboy]# \rm t.sh
[root@VM_179_129_centos oldboy]# ls
alex.txt oldboy.txt test test.sh
#发现t.sh成功被删除了
[root@VM_179_129_centos oldboy]# /bin/rm alex.txt
[root@VM_179_129_centos oldboy]# ls
oldboy.txt test test.sh