【Linux】alias别名



前言

alias别名,可通过对某些命令操作通过使用别名的方式直接去执行。


一、系统内置别名

[root@asdfv ~]# 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'

二、临时别名

在命令行中定义的别名,仅对当前shell进程有效,一旦exit退出终端或系统重启,该别名对应的命令操作将失效。

[root@localhost ~]# cat test-alias.txt
alias test 2022
this is a test txt!
[root@localhost ~]# alias cattxt='cat test-alias.txt'
[root@localhost ~]# cattxt
alias test 2022
this is a test txt!

# 退出再进入
Last login: Mon May 30 20:33:45 2022 from gateway
[root@localhost ~]# cattxt
-bash: cattxt: command not found

若想删除临时别名,可通过unalias取消

[root@asdfv ~]# unalias cattxt
[root@asdfv ~]# cattxt
-bash: cattxt: command not found

三、永久别名

若需要永久有效,则要定义在配置文件中:

  • ~/.bashrc 仅对当前用户有效
  • /etc/bashrc 对所有用户有效

上述两个文件在修改完成后,都需要经source命令使其生效。

[root@asdfv ~]# vi ~/.bashrc 
      1 # .bashrc
      2 
      3 # User specific aliases and functions
      4 
      5 alias rm='rm -i'
      6 alias cp='cp -i'
      7 alias mv='mv -i'
      8 alias cattxt='cat test-alias.txt'
      9 
     10 # Source global definitions
     11 if [ -f /etc/bashrc ]; then
     12         . /etc/bashrc
     13 fi
[root@asdfv ~]# source ~/.bashrc 

测试是否生效,退出再进入后,cattxt可直接执行输出内容,但一旦切换到另一个用户后,该别名命令就失效,因为前面修改的~/.bashrc仅对当前用户(root)有效。

Last login: Sat Jun 11 00:29:14 2022 from gateway
[root@asdfv ~]# cattxt
alias test 2022
this is a test txt!

[root@asdfv ~]# su - user1
Last login: Thu Jun  2 12:11:29 CST 2022 on pts/2
[user1@asdfv ~]$ cattxt
-bash: cattxt: command not found
[user1@asdfv ~]$ 

总结

在日常使用Linux中,对一些较深目录中文件,或常用的操作,可使用alias别名来生成简短的别名命令。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值