【Linux】初见“which命令”,“find命令”以及linux执行命令优先级

1.which命令

查找命令文件存放目录

搜索范围由环境变量PATH决定(echo $PATH)

在这里插入图片描述

which命令格式:which 命令|程序名 //默认当找到第一个目标后不再继续查找

或者:

which -a 命令|程序名 //在所有搜索路径中查找

注意:

使用which查找内部命令时,将找不到对应的程序;

which搜索命令查不到路径的两种情况:

1.该命令是内部命令;

2.该命令是外部命令,但该外部命令没有放置到PATH环境变量所指定的路径中;

which查询外部命令没有放置到PATH环境变量路径中的解决方法:

1.用绝对路径或相对路径将外部命令手动添加到系统中默认的路径中(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:)

2.先进入系统目录,将外部命令复制到指定目录中

[root@clr ~]# which ls   #查看ls命令在系统中的位置
alias ls='ls --color=auto'
	/usr/bin/ls
[root@clr ~]# alias
alias cp='cp -i'
alias ls='ls --color=auto'

[root@clr ~]# which history    #查看内部命令history在系统中的位置
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

[root@clr ~]# which java  #查看外部命令java在系统中的位置
/usr/bin/java

[root@clr ~]# which vlan     # #查看内部命令vlan在系统中的位置(内部命令在系统中无法查询所在位置)
/usr/bin/which: no vlan in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

which -a命令

在这里插入图片描述

1.1 whereis命令

whereis:查看文件的位置;

whereis用于显示命令及相关文件的路径位置信息.

能够找到命令(二进制程序)、命令源代码、man帮助手册等相关的文件路径位置信息.

补充:

whereis命令有别于find命令进行的全盘搜索,whereis命令查找速度非常快,因为它不是在磁盘中乱找,而是在指定数据库中查询,该数据库是Linux系统自动创建的,包含有本地所有文件的信息,每天自动更新一次。但正因为这样,whereis命令的搜索结果会不即时,比如刚添加的文件可能搜不到,原因就是该数据库文件没有被更新,管理人员手动执行updatedb命令进行更新即可。

whereis命令格式:whereis [参数] 命令名

常用参数:

在这里插入图片描述

whereis -b命令

[root@clr ~]# whereis -b svn   #查找svn的相关信息
svn: /usr/bin/svn   #返回svn在系统中的位置
[root@clr ~]# whereis -b tcp  #查找tcp的相关信息
tcp:[root@clr ~]# whereis -b tomcat     #查找tomcat的相关信息
tomcat:[root@clr ~]# whereis -b poweroff     #系统中并未查询到tcp和tomcat的相关信息
poweroff: /usr/sbin/poweroff   #返回poweroff在系统中的位置

1.2 locate命令

locate:配合数据库查看文件位置;

locate 通过搜寻系统内建文档数据库快速找到档案,默认情况下 locate 命令在搜寻数据库时,比从整个硬盘搜寻资料快得多,但较差劲的是 locate 所找到的档案若是最近才建立或刚更名的,可能会找不到。

locate 与 find 命令相似,可以使用如 、? 等进行正则匹配查找

[root@clr ~]# locate pwd   #查找和pwd 相关的所有文件(文件名中包含 pwd)
/etc/.pwd.lock
/usr/bin/pwd
/usr/bin/pwdx
/usr/include/pwd.h
/usr/lib/modules/3.10.0-1160.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko.xz
/usr/lib64/cracklib_dict.pwd
/usr/lib64/python2.7/lib-dynload/spwdmodule.so
/usr/lib64/python2.7/site-packages/ldap/controls/pwdpolicy.py
/usr/lib64/python2.7/site-packages/ldap/controls/pwdpolicy.pyc


[root@clr ~]# locate /etc/sh  #搜索 etc 目录下所有以 sh 开头的文件
/etc/shadow
/etc/shadow-
/etc/shells

1.3 搜索文件命令总结

which:查看可执行文件的位置;

whereis:查看文件的位置;

locate:配合数据库查看文件位置;

find:实际搜寻硬盘查询文件名称

2.find命令

采用递归方式,根据目标的名称、类型、大小等不同属性进行精细查找

find命令格式

在这里插入图片描述

常用查找条件类型

在这里插入图片描述

注意:-type选项中,不加f/d时,默认会将文件和目录都查找并显示出来。

find -name命令

[root@clr ~]# cd /boot/
[root@clr /boot]# ls
config-3.10.0-1160.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
initramfs-3.10.0-1160.el7.x86_64.img

[root@clr /boot]# ll -h  #以人性化长格式方式详细显示/boot目录下的子目录以及文件
总用量 121M
-rw-r--r--. 1 root root 150K 1020 2020 config-3.10.0-1160.el7.x86_64
drwx------. 3 root root   17 729 2020 efi
drwxr-xr-x. 2 root root   27 323 00:42 grub
drwx------. 5 root root   97 323 00:54 grub2
-rw-------. 1 root root  77M 323 00:44 initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
-rw-------. 1 root root  28M 323 00:54 initramfs-3.10.0-1160.el7.x86_64.img

[root@clr /boot]# find ./ -name "initramfs*" #查找当前目录下以"initramfs"字符串开头的文件 
./initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
./initramfs-3.10.0-1160.el7.x86_64.img

find -size命令

[root@clr /boot]# find /boot/ -size +10M #查找并显示/boot/目录下文件大小大于10M的文件
/boot/initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
/boot/initramfs-3.10.0-1160.el7.x86_64.img

各表达式之间使用逻辑运算符

  • “-a” 表示 而且 (and)

  • “-o” 表示 或者(or)

find -a命令

[root@clr /boot]# find /boot/ -size -5M -a -type f  #查找并显示/boot目录下小于5M的文件
/boot/efi/EFI/centos/BOOT.CSV
/boot/efi/EFI/centos/BOOTX64.CSV
/boot/efi/EFI/centos/MokManager.efi
/boot/efi/EFI/centos/mmx64.efi
/boot/efi/EFI/centos/shim.efi
/boot/efi/EFI/centos/shimx64-centos.efi
/boot/efi/EFI/centos/shimx64.efi
/boot/efi/EFI/centos/fwupia32.efi
/boot/efi/EFI/centos/fwupx64.efi

find -o命令

[root@clr /boot]# find ./ -name "*.img" -o -name "vmlinuz*"  #查找当前目录下,以“.img”结尾,或者以"vmlinuz"字符串开头的文件和目录 
./grub2/i386-pc/core.img
./grub2/i386-pc/boot.img
./vmlinuz-3.10.0-1160.el7.x86_64
./initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
./vmlinuz-0-rescue-675c23e85f174b7ca25013149d0b69ad
./initramfs-3.10.0-1160.el7.x86_64.img

find -type f命令

[root@clr /boot]# find /boot/ -size +20M -a -name "*.img" -a -type f  #查看并显示/boot/目录下以“.img”结尾,并且超过20M大小的文件
00/boot/initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
/boot/initramfs-3.10.0-1160.el7.x86_64.img

find -mtime命令

[root@clr ~]# find ./ -mtime +2 -exec ls -l {} \;#查看并显示当前目录下2天以前的文件和目录,将该查询的结果作为参数传递给-exec命令,最后以长格式方式显示出来
-rw-r--r--. 1 root root 18 1229 2013 ./.bash_logout
-rw-r--r--. 1 root root 176 1229 2013 ./.bash_profile
-rw-r--r--. 1 root root 176 1229 2013 ./.bashrc
-rw-r--r--. 1 root root 100 1229 2013 ./.cshrc
-rw-r--r--. 1 root root 129 1229 2013 ./.tcshrc
-rw-------. 1 root root 1832 323 00:54 ./anaconda-ks.cfg
总用量 12
drwxr-xr-x. 2 root root    52 329 15:07 abrt
drwx------. 2 root root    18 323 00:55 dconf
-rw-r--r--. 1 root root 12288 323 03:33 event-sound-cache.tdb.675c23e85f174b7ca25013149d0b69ad.x86_64-redhat-linux-gnu
[root@clr ~]# find ./ -mtime +30 -exec ls -l {} \;#查看并显示当前目录下一个月以前的文件和目录,将该查询的结果作为参数传递给-exec命令,最后以长格式方式显示出来
-rw-r--r--. 1 root root 18 1229 2013 ./.bash_logout
-rw-r--r--. 1 root root 176 1229 2013 ./.bash_profile
-rw-r--r--. 1 root root 176 1229 2013 ./.bashrc
-rw-r--r--. 1 root root 100 1229 2013 ./.cshrc
-rw-r--r--. 1 root root 129 1229 2013 ./.tcshr
[root@clr ~]# find ./ -mtime +30 -exec rm -f {} \;
#查看当前目录下一个月以前的文件和目录,并将该查询结果作为参数传递给-exec命令,最后强制删除
[root@clr ~]# find ./ -mtime -30 -exec ls -l {} \;#查看当前目录下30天以内的文件和目录,并将该查询结果作为参数传递给-exec命令,最后显示出来
总用量 32
drwxr-xr-x. 2 root root   20 328 14:11 123.txt
-rw-r--r--. 1 root root   14 327 23:09 2.txt.bz2
-rw-r--r--. 1 root root    0 327 23:09 3344.txt
-rw-r--r--. 1 root root    4 328 14:12 456.txt
-rw-r--r--. 1 root root 3076 328 14:26 abc456
-rw-r--r--. 1 root root    0 328 16:36 abc.tat
-rw-r--r--. 1 root root   74 328 22:37 abc.txt
-rw-------. 1 root root 1832 323 00:54 anaconda-ks.cfg
drwxr-xr-x. 6 root root 4096 327 16:37 gzy
[root@clr ~]# find ./ -mtime 2  -exec ls -l {} \;#查看当前目录下2天之前,一天内的文件和目录,并将该查询结果作为参数传递给-exec命令,最后显示出来
总用量 8
-rw-r--r--. 1 root root 1063 327 08:41 log
-rw-r--r--. 1 root root 1063 324 16:01 log.bak
-rw-r--r--. 1 root root 1063 327 08:41 ./.cache/imsettings/log
总用量 3316
-rw-r--r--. 1 root root      11 323 01:07 db-locale.txt
-rw-r--r--. 1 root root       2 323 01:07 db-version.txt
-rw-r--r--. 1 root root       6 323 01:07 first-index.txt
-rw-r--r--. 1 root root      10 323 01:07 last-crawl.txt
-rw-r--r--. 1 root root      11 327 08:41 locale-for-miner-apps.txt
-rw-r--r--. 1 root root      11 327 08:41 locale-for-miner-user-guides.txt
-rw-r--r--. 1 root root 1634304 323 11:04 meta.db
-rw-r--r--. 1 root root   32768 327 23:54 meta.db-shm
-rw-r--r--. 1 root root 1338328 327 23:54 meta.db-wal
-rw-r--r--. 1 root root  356412 323 01:07 ontologies.gvdb
-rw-r--r--. 1 root root      40 323 01:07 parser-sha1.txt
-rw-r--r--. 1 root root 11 327 08:41 ./.cache/tracker/locale-for-miner-user-guides.txt
-rw-r--r--. 1 root root 11 327 08:41 ./.cache/tracker/locale-for-miner-apps.txt

find -delete用法

[root@clr /boot]# find ./ -name "*.img" -delete #查看当前目录下以“.img”结尾的文件和目录,并且将该查询结果删除

find -inmu命令

[root@clr /boot]# ll -i  #查看当前目录下,目录和文件的inode号
总用量 123624
  1234000 -rw-r--r--. 1 root root   153591 1020 2020 config-3.10.0-1160.el7.x86_64
 33556200 drwx------. 3 root root       17 729 2020 efi
101413155 drwxr-xr-x. 2 root root       27 323 00:42 grub
 67146888 drwx------. 5 root root       97 323 00:54 grub2
  2752025 -rw-------. 1 root root 79724349 323 00:44 initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
  1275566 -rw-------. 1 root root 29226758 323 00:54 initramfs-3.10.0-1160.el7.x86_64.img
  1234001 -rw-r--r--. 1 root root   320648 1020 2020 symvers-3.10.0-1160.el7.x86_64.gz
  1233999 -rw-------. 1 root root  3616707 1020 2020 System.map-3.10.0-1160.el7.x86_64
  2752000 -rwxr-xr-x. 1 root root  6769256 323 00:44 vmlinuz-0-rescue-675c23e85f174b7ca25013149d0b69ad
  1234002 -rwxr-xr-x. 1 root root  6769256 1020 2020 vmlinuz-3.10.0-1160.el7.x86_64
  
[root@clr /boot]# find ./ -inum 2752000  #查找当前目录下inode号为2752000的文件或目录
./vmlinuz-0-rescue-675c23e85f174b7ca25013149d0b69ad

2.1 find之exec用法

  • -exec参数后面跟的是Linux命令,它是以分号“;”为结束标志,由于各个系统中分号会有不同的意义,因此在分号前面加上反斜杠转义符“\”

  • {}代表前面find查找出来的文件名

find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用Ils -l命令将它们列出;

find ./ -type f -exec ls -l {} ;

[root@clr /boot]# find /boot/ -size +20M -a -name "*.img" -a -type f -exec ls -l {} \; ##查找当前目录下,以“.img”结尾,或者以"vmlinuz"字符串开头的文件和目录,并将查询结果作为参数传递给-exec命令,最后以长格式方式显示查询出来的文件信息
-rw-------. 1 root root 79724349 323 00:44 /boot/initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
-rw-------. 1 root root 29226758 323 00:54 /boot/initramfs-3.10.0-1160.el7.x86_64.img

在这里插入图片描述

2.2 管道符之xargs用法

[root@clr /boot]# find ./ -name "*.img" | xargs ls -l #查看并显示当前目录下以“.img”结尾的文件和目录,并将该查询结果作为参数传递给管道符的xargs,最后以长格式方式显示出来
-rw-r--r--. 1 root root      512 323 00:45 ./grub2/i386-pc/boot.img
-rw-r--r--. 1 root root    26852 323 00:45 ./grub2/i386-pc/core.img
-rw-------. 1 root root 79724349 323 00:44 ./initramfs-0-rescue-675c23e85f174b7ca25013149d0b69ad.img
-rw-------. 1 root root 29226758 323 00:54 ./initramfs-3.10.0-1160.el7.x86_64.img

3 Linux常用命令

在这里插入图片描述

4.命令执行优先级

第一优先级:指定路径的命令绝对路径 /usr/bin/ls 或者相对路径 cd /usr/bin ./ls

第二优先级:别名指定的命令 alias myls=‘/usr/bin/ls -alh’

第三优先级:内部命令

第四优先级:hash 命令

linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样。第一次执行命令shell解释器默认的会从PATH路径下寻找该命令的路径,当你第二次使用该命令时,shell解释器首先会查看hash表,没有该命令才会去PATH路径下寻找。hash表能提高命令的调用速率

第五优先级:通过PATH 定义的路径顺序查找

如果以上顺序都找不到,就会报“未找到命令…”的错误。

猜你喜欢

转载自blog.csdn.net/cailirong123/article/details/129838354