系统命令
目录
-
内部命令
1.1. enable -
外部命令
2.1. which -a CMD
2.2. where CMD -
hash表
-
PATH变量
-
alias别名
5.1. ~/.bashrc
5.2. /etc/bashrc
5.3. 配置文件生效
5.4. 忽略别名使用原始命令\CMD -
系统命令查询顺序
内部命令
- 由shell自带的,而且通过某命令形式提供
- 内部命令放在内存中,这样效率高,直接调用
- 每一种shell的内部命令不一样
查看内部命令
- enable查看内部命令
- enable cmd 启用内部命令
- enable -n cmd 禁止内部命令
- enable -n 查看禁用列表
[root@centos7 cd]#enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
......
外部命令
- 在系统中有相对应的可执行文件的程序
- 查看外部命令的路径
- which -a CMD
- whereis CMD
[root@centos7 cd]#which -a cat
/usr/bin/cat
[root@centos7 cd]#whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz
hash缓存表
用来存放使用过的外部命令,以便下次使用的时候优先从hash中调取,不用去磁盘中寻找,提高效率,如果hash中没有相对应的命令,才会去PATH变量中查找
- 系统刚启动的时候,hash表为空
- 常用命令
hash命令 | 说明 |
---|---|
hash | 显示缓存 |
hash -l | 显示缓存,可作为输入使用 |
hash -r | 清楚缓存 |
[root@centos7 cd]#hash -l
builtin hash -p /usr/bin/tty tty
builtin hash -p /usr/bin/df df
builtin hash -p /usr/bin/findmnt findmnt
builtin hash -p /usr/sbin/findfs findfs
builtin hash -p /usr/sbin/blkid blkid
builtin hash -p /usr/bin/cat cat
builtin hash -p /usr/bin/whereis whereis
builtin hash -p /usr/sbin/lsof lsof
builtin hash -p /usr/bin/who who
builtin hash -p /usr/bin/ls ls
builtin hash -p /usr/sbin/fuser fuser
builtin hash -p /usr/bin/w w
builtin hash -p /usr/sbin/lspci lspci
[root@centos7 cd]#hash
hits command
1 /usr/bin/tty
2 /usr/bin/df
1 /usr/bin/findmnt
7 /usr/sbin/findfs
1 /usr/sbin/blkid
1 /usr/bin/cat
4 /usr/bin/whereis
1 /usr/sbin/lsof
1 /usr/bin/who
3 /usr/bin/ls
2 /usr/sbin/fuser
1 /usr/bin/w
1 /usr/sbin/lspci
[root@centos7 cd]#hash -r
[root@centos7 cd]#hash
hash: hash table empty
PATH变量
系统从PATH变量中的路径按顺序逐一查找,找到对应的路径目录之后就不会继续往下搜索,即可执行程序若想直接使用,必须放在PATH变量的某个路径中,或者把该程序的路径加到PATH变量中
[root@centos7 cd]#echo $PATH
/app/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
命令别名
- alias显示所有命令别名
- 若别名与系统命令名相同,如"ls"命令,若想使用原始命令,在命令前加,如"\ls"这样就可以忽略别名
- 定义别名,直接使用该命令仅保存在shell内存中
alias NAME=“VALUE” 或 alias NAME=‘VALUE’
[root@centos7 cd]#alias
alias baketc='cp -avr /etc/ /data/backup2018-08-13/'
alias cdate='cd /data/root2018-08-13'
alias cdb='cd /root/bin'
alias cdnet='cd /etc/sysconfig/network-scripts'
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 rn='systemctl restart network'
alias scandisk='echo "- - -" > /sys/class/scsi_host/host0/scan'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
- 让别名永久生效,需要写入配置文件
- 当前用户生效 :~/.bashrc (一般建议写入自己用户的文件)
- 所有系统有效 :/etc/bashrc
- 配置之后不会立刻生效,需要重新读取配置文件
source ~/.bashrc 或 . ~/.bashrc
[root@centos7 cd]#cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cdnet="cd /etc/sysconfig/network-scripts"
alias baketc="cp -avr /etc/ /data/backup$(date +%F)/"
alias vi="vim"
alias cdate="cd /data/root`date +%F`"
alias scandisk='echo "- - -" > /sys/class/scsi_host/host0/scan'
alias rn="systemctl restart network"
alias cdb="cd /root/bin"
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos7 ~]#source .bashrc
[root@centos7 ~]#. .bashrc
系统如何寻找到命令?
alias --> 内部命令 --> hash表 --> 外部命令(查PATH变量)