概述
查找类的命令:
which和whereis 只能查找Linux里的命令,精确查找,到PATH变量的路径查找
locate:可以查找命令和普通文件或文件夹,模糊查找,到自己的数据库里查找(整个/目录下查找)
which,whereis,locate都是根据名字查找
查找:
1.精确查找:必须一模一样
2.模糊查找:只要包含就可以了
whereis 可以查询命令存放的路劲,同时还告诉我们man手册的路径
[root@jack /]# whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
[root@jack /]# which mkdir
/usr/bin/mkdir
[root@jack /]# man mkdir --》背后就是去查询了mkdir的使用手册
whereis的查询范围也是PATH变量的指定范围吗?
是
shell编程里区分大小写
如何知道一个命令是通过那个软件包安装过来的?
[root@jack /]# yum provides locate
mlocate-0.26-20.el8.x86_64 : An utility for finding files by name
仓库 :baseos
匹配来源:
文件名 :/usr/bin/locate
baseos 这个仓库里存放的都是centos系统基础软件
utility 工具,使用程序
[root@jack /]# yum install mlocate -y
# locate命令是到/var/lib/mlocate/mlocate.db 数据库文件里查找文件的
[root@jack /]# locate passwd
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录
[root@jack guangdong]# cd /var/lib/mlocate/
[root@jack mlocate]# ls
[root@jack mlocate]# updatedb # 帮助我们创建mlocate.db数据库文件
[root@jack mlocate]# ls
mlocate.db
[root@jack mlocate]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/etc/ssh/passwd
/hunantv/wanghan/passwd
/jindafu/boot/passwd
/lianxi/passwd
/lianxi2/passwd
/pem/passwd
如果是文件,又改如何查找呢?
1.将Linux里超过100M的文件查找出来,复制到/backup
find / -type f -size +100M -exec cp {} /backup ;
2.查找出/backup目录下创建时间是7天前的并且大于500M的文件删除find /backup -type f (-mtime +7 -a -size +500M) -exec rm -rf {} ;
find:
对象:文件夹和文件,范围:/,时效性:实时
条件:
名字 -name 区分大小写
-iname 不区分大小写
Like -name, but the match is case insensitive.
insensitive:不敏感的
时间 -mtime n
File's data was last modified n*24 hours ago.
-mtime +7 7个24小时前
+mtime -7 7个24小时内
-mmin n
大小 -size +10M 大于10M -10M 小于10M k-M-G-T-P-E
类型:
文件类型:
d 文件夹(目录)directory
f 普通文件 file
l 链接文件 link
c 字符设备文件 character
b 块设备文件 block
s socket文件
p pipe管道文件
动作:查找出来文件或文件夹后,可删除,复制,移动等操作
-exec --》execute 执行
-ok 要求确认 --》不推荐
[root@jack dongguang]# find / -name "*zengqinlin*" -type f -exec cp {} /lianxi/hunan/dongguang ;[root@jack dongguang]# ls
shaoyao_zengqinlin1 shaoyao_zengqinlin2 shaoyao_zengqinlin4 shaoyao_zengqinlin6 shaoyao_zengqinlin8 shaoyao_zengqinlin10 shaoyao_zengqinlin3 shaoyao_zengqinlin5 shaoyao_zengqinlin7 shaoyao_zengqinlin9
[root@jack guangdong]# find / -name "*zengqinlin*"
/lianxi2/hunan/guangdong/shaoyao_zengqinlin1
/lianxi2/hunan/guangdong/shaoyao_zengqinlin2
/lianxi2/hunan/guangdong/shaoyao_zengqinlin3
/lianxi2/hunan/guangdong/shaoyao_zengqinlin4
/lianxi2/hunan/guangdong/shaoyao_zengqinlin5
/lianxi2/hunan/guangdong/shaoyao_zengqinlin6
/lianxi2/hunan/guangdong/shaoyao_zengqinlin7
/lianxi2/hunan/guangdong/shaoyao_zengqinlin8
/lianxi2/hunan/guangdong/shaoyao_zengqinlin9
/lianxi2/hunan/guangdong/shaoyao_zengqinlin10
[root@jack guangdong]# find . -type f
./a.txt
./shaoyao_zengqinlin1
./shaoyao_zengqinlin2
./shaoyao_zengqinlin3
./shaoyao_zengqinlin4
./shaoyao_zengqinlin5
./shaoyao_zengqinlin6
./shaoyao_zengqinlin7
./shaoyao_zengqinlin8
./shaoyao_zengqinlin9
./shaoyao_zengqinlin10
[root@jack guangdong]# find /boot -size +10
/boot/grub2/i386-pc
/boot/grub2/i386-pc/gcry_md5.mod
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/gcry_rijndael.mod
/boot/grub2/i386-pc/affs.mod
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/afs.mod
[root@jack dev]# find / -name "*.txt" -type f
[root@jack dev]# find /lianxi -type d
[root@jack dev]# find /lianxi -type f |wc -l
63
[root@jack dev]# find / -iname "*.conf" -type f
[root@jack dev]# find /boot -size +3M -name "*linuz*"
/boot/vmlinuz-4.18.0-240.el8.x86_64
/boot/vmlinuz-0-rescue-4ab2178d3ded4717826d51ba848b92a3
[root@jack guangdong]# find . -mtime -3
.
./a.txt
./zengqilin.txt
./shaoyao_zengqinlin1
./shaoyao_zengqinlin2
./shaoyao_zengqinlin3
./shaoyao_zengqinlin4
./shaoyao_zengqinlin5
./shaoyao_zengqinlin6
./shaoyao_zengqinlin7
./shaoyao_zengqinlin8
./shaoyao_zengqinlin9
./shaoyao_zengqinlin10
[root@jack guangdong]# find . -mmin +20
.
./a.txt
./zengqilin.txt
./shaoyao_zengqinlin1
./shaoyao_zengqinlin2
./shaoyao_zengqinlin3
./shaoyao_zengqinlin4
./shaoyao_zengqinlin5
./shaoyao_zengqinlin6
./shaoyao_zengqinlin7
./shaoyao_zengqinlin8
./shaoyao_zengqinlin9
./shaoyao_zengqinlin10
[root@jack guangdong]# touch c.txgt
[root@jack guangdong]# find . -newer b.txt
.
./c.txgt
[root@jack guangdong]# find / -user hejin
find: ‘/proc/23325/task/23325/fd/8’: 没有那个文件或目录
find: ‘/proc/23325/task/23325/fdinfo/8’: 没有那个文件或目录
find: ‘/proc/23325/fd/7’: 没有那个文件或目录
find: ‘/proc/23325/fdinfo/7’: 没有那个文件或目录
/var/spool/mail/hejin
/home/hejin
/home/hejin/.bash_logout
/home/hejin/.bash_profile
/home/hejin/.bashrc
/home/hejin/hunan
/home/hejin/hunan/changsha
/home/hejin/.sanchuang
/home/hejin/.bash_history
/lianxi2/hunan/guangdong/a.txt
[root@jack dongguang]# touch a{1..10}.txt
[root@jack dongguang]# ls
a10.txt a1.txt a2.txt a3.txt a4.txt a5.txt a6.txt a7.txt a8.txt a9.txt
[root@jack dongguang]# find /lianxi2/hunan/dongguang -name "*.txt" -type f -exec cp {} /backup ;
[root@jack dongguang]# cd /backup
[root@jack backup]# ls
20220318195327-log.tar.gz a1.txt a3.txt a5.txt a7.txt a9.txt china
a10.txt
[root@jack backup]# find /backup -name "*.txt" -type f -exec rm -rf {} /lianxi2/hunan/dongguang ;
[root@jack backup]# ls
20220318195327-log.tar.gz backup_log.sh china
-exec 采取动作
cp {} /backup ; -->{} 代表前面的find命令查找到的符合条件的文件
; 代表-exec选项执行的命令的结束标志
man find
/mtime 查找mtime
n 下一个符合条件的 next
N 上一个符合条件的
条件的组合:
-a and 逻辑与
-o or 逻辑或
-not ! 逻辑非 取反
find /home -user root -type f ( -size +2k -o -name "*cali*" )
最后
以上就是深情诺言为你收集整理的Linux || 查询类命令的全部内容,希望文章能够帮你解决Linux || 查询类命令所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复