Linux操作系统文件查找

++++++++++++++++++++++++++++++++++++++++++++++++
标题:Linux操作系统的文件或命令查找
内容:命令查找(which和whereis)、文件查找(locate和find)
时间:2019年4月15日
++++++++++++++++++++++++++++++++++++++++++++++++
1. 系统命令文件查找
[root@test ~]# which yum //会遍历环境变量的是否存在
/usr/bin/yum
[root@test ~]# whereis yum //遍历环境变量、遍历man手册
yum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8.gz


2. 常规文件查找
2.1 locate常规文件查找
[root@test ~]# mkdir /dir100
[root@test ~]# touch /dir100/file001
[root@test ~]# locate /dir100/ file001 //数据库未更新
[root@test ~]# updatedb //手动更新数据库
[root@test ~]# locate /dir100/ file001
/dir100/file001
[root@test ~]# rm -rf /dir100/file001
[root@test ~]# locate /dir100/ file001 //删除文件后数据库未更新
/dir100/file001
[root@test ~]# updatedb
[root@test ~]# locate /dir100/ file001
locate知识点总结:
locate是通过数据库进行比对查找,查找速度非常快,但结果不一定准确。
locate配置的数据库位置"/var/lib/mlocate/mlocate.db"。
locate查询系统固定的文件效率会很高。
locate使用的数据库系统每日会自动更新一次。
使用updatedb命令手工更新数据库可能造成大量的IO操作,增加系统负担。

2.2 find常规文件查找
语法结构:find [options] [path] [expression]
++文件名称查找++
[root@test ~]# find /etc/ -name 'ifcfg-eth0'
[root@test ~]# find /etc/ -iname 'ifcfG-eth0' //忽略文件名称大小写
[root@test ~]# find /etc/ -name '*eth0*'
++文件属主属组以及类型查找++
[root@test ~]# find / -user mysql -name 'file*'
[root@test ~]# find / -group mysql -type d -iname 'MYSQL'
[root@test ~]# find / -type p |head -n 1
++文件大小查找++
[root@test ~]# find /etc/ -size 5M
[root@test ~]# find /etc/ -size +5M
[root@test ~]# find /etc/ -size -5M
++文件时间查找++
[root@test ~]# find /dir100/ -atime -1 //访问时间--realtime
[root@test ~]# find /dir100/ -mtime 1 //内容修改时间
[root@test ~]# find /dir100/ -ctime +1 //属性修改时间
++按文件所在深度++
[root@test ~]# find /etc/ -maxdepth 4 -iname 'ifcfg-eth0'
[root@test ~]# find /etc/ -mindepth 5 -iname 'ifcfg-eth0' //什么都没有
++按权限查找++
[root@test ~]# find /dir100/ -perm 222 //权限等于
[root@test ~]# find /dir100/ -perm -0111 //权限包含
++其他高级用法++
[root@test ~]# time find / -iname "ifcfg-eth0" //效率较低
[root@test ~]# time find 'ls /' -iname "ifcfg-eth0" //效率较高

[root@test ~]# find /etc/ -regex '.*ifcfg-eth[0-9]' //使用正则表达式匹配

[root@test ~]# find /home/ -nogroup -o -nouser //无主对象
[root@test ~]# find /home/ \( -nogroup -o -nouser \) -delete //删除无主对象
[root@test ~]# find /etc/ \( -size +5M -a -size -10M \) -print
[root@test ~]# find /etc/ -iname 'ifcfg-eth0' -ls //长格式显示,类似于ls -l,但不同于ls -l

[root@test ~]# find /dir100/ -iname 'file*' -exec cp {} /tmp \; //强制覆盖
[root@test ~]# find /dir100/ -iname 'file*' -ok cp {} /tmp \; //覆盖提示

[root@test ~]# find /tmp/ -name 'file*' -exec rm -rf {} \; //等于rm -rf file1;rm -rf file2
[root@test ~]# find /tmp/ -name 'file*' -exec rm -rf {} \+ //等于rm -rf file1 file2

[root@test ~]# find /etc/ -iname 'ifcfg-eth0'|xargs ls //存在部分命令不接受管道传递的信息,需要则使用xargs参数
[root@test ~]# find /etc/ -iname 'ifcfg-eth0'|xargs -I {} cp {} /tmp

猜你喜欢

转载自www.cnblogs.com/lv1572407/p/10753958.html