前言
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
别名来生成简短的别名命令。