20.文件查找

一 文件查找

grep: 文件内容过滤

[root@localhost ~]# grep 'root' /etc/passwd     //从/etc/passwd文件中过滤root字段

find: 文件查找,针对文件名

1.1 命令文件 # which ls //从PATH环境变量 (echo $PATH)

[root@localhost ~]# whereis vim     //只能查找系统本身的二进制程序文件

[root@localhost ~]# echo $PATH  //显示环境变量    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1.2、任意文件

A. locate

(查询的数据库: /var/lib/mlocate/mlocate.db)

手动更新数据库:updatedb

updatedb后才能查找到 非常麻烦 不建议使用 如果没有 locate 使用YUM直接安装是不行的。

要查一下在哪个包里

[root@fanhua ~]# yum provides locate            //查找locate安装包
[root@fanhua ~]# yum -y install mlocate
[root@fanhua ~]# updatedb                           //更新数据库
[root@fanhua ~]# locate a.txt               //查找a.txt(只要文件有a.txt就会被查找到)

二 find详解

find 目录 条件选项 表达式

===expression=== 熟用*通配符

2.1按文件名:

[root@fanhua ~]# find /etc -name "ifcfg-ens33"          //以名字的方式查找 
[root@fanhua ~]# find /etc -iname "ifcfg-ens33"         //-i忽略大小写
[root@fanhua ~]# find /etc -iname "ifcfg-ens*"

2.2按文件大小:

[root@fanhua ~]# find /etc -size +5M                        //大于5M
[root@fanhua ~]# find /etc -size 5M                     //等于5M
[root@fanhua ~]# find /etc -size -5M
[root@fanhua ~]# find /etc -size +5M -ls                   //-ls找到的处理动作,不是平时用的ls

2.3指定查找的目录深度:

-maxdepth levels
-mindepth levels
[root@fanhua ~]# find / -maxdepth 3 -a  -name "ifcfg-ens33"           //maxdepth 3   最大3层    a要满足2个条件 并且
按时间找(atime,mtime,ctime):
[root@fanhua ~]# find /etc -mtime +5                        //修改时间超过5天
[root@fanhua ~]# find /etc -mtime 5                     //修改时间等于5天
[root@fanhua ~]# find /etc -mtime -5                        //修改时间5天以内

2.4按文件类型:

[root@fanhua ~]# find /dev -type f                         //f普通
[root@fanhua ~]# find /dev -type d                         //d目录
[root@fanhua ~]# find /dev -type l                         //l链接
[root@fanhua ~]# find /dev -type b                         //b块设备
[root@fanhua ~]# find /dev -type c                         //c字符设备
[root@fanhua ~]# find /dev -type s                         //s套接字 

2.5按文件权限:

[root@fanhua ~]# find . -perm 644 -ls            .是当前目录    精确查找644  

[root@fanhua ~]# find . -perm -644 -ls     -是包含到意思 
带不带- 自己对比一下   查看。       带-表示只要有6就可以

[root@fanhua ~]# find . -perm -600 -ls
[root@fanhua ~]# find . -perm -222 -ls                  //可写
[root@fanhua ~]# find /usr/bin  -perm -4000 -ls       //包含set uid
[root@fanhua ~]# find /usr/bin  -perm -2000 -ls       //包含set gid
[root@fanhua ~]# find /usr/bin  -perm -1000 -ls       //包含sticky    
找到后处理的动作 ACTIONS:

-name "ifcfg*" | xargs

-name "ifcfg*" -print

-name "ifcfg*" -ls
[root@fanhua ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; 

exec命令用于调用并执行指令的命令  查找带root带文件   复制到tmp下 

[root@fanhua ~]# find /etc -name “root*”  -exec cp -rf {} /tmp \;              
 复制到当前文件下        /tmp换成. 

[root@fanhua ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;    exec为执行一条shell命令      {}为前面的东西   \; 格式

[root@fanhua ~]# find /etc -name "ifcfg*" -delete                       
               -delete  -ls -print    为函数
               函数不能用传参

    扩展知识:find结合xargs*

[root@fanhua ~]# find . -name "yang*.txt" |xargs rm -rf    
!!!!!!!!!!!!!重点 找到之后删除处理xargs 参数传递处理找出后删除

[root@fanhua ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

案例1: 分别找出file5 和除了file5的文件

[root@fanhua ~]# mkdir dir1
[root@fanhua ~]# touch dir1/file{1..20}

[root@fanhua ~]# find /root/dir1 -name "file5"
[root@fanhua ~]# find /root/dir1 ! -name "file5"        !为取反

[root@fanhua ~]# find /root/dir1 -name "file5" -o -name "file9"     即是file5又是file9
/root/dir1/file5
/root/dir1/file9

三.exec和xargs的区别

find . -name 'core' -type f -exec rm {} ; 时,find -exec 命令会对每个匹配的文件执行一个单独的rm操作(execute a separate rm for each one), 正如你手动敲入下面命令:

[root@fanhua ~]# rm ./bin/core
[root@fanhua ~]# rm ./source/shopping_cart/core
[root@fanhua ~]# rm ./backups/core

但是使用这种方式,如果有100个文件匹配了,那么就需要启100个进程,一个进程处理一个rm命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给rm不就可以了吗?也就是说

[root@fanhua ~]# rm ./bin/core
[root@fanhua ~]# rm ./source/shopping_cart/core 
[root@fanhua ~]# rm ./backups/core
改成: rm ./bin/core ./source/shopping_cart/core ./backups/core但是前提是后面的命令必须支持多参数。相有些命令,比如unzip,就不支持输入多个jar包,所以必须用-exec。

xargs,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。因此上面的find -exec命令可以改写成:find . -name 'core' -type f -print | xargs rmWith this approach, xargs bundles together as many filename arguments as possible for submission to each invocation of rm that's needed

相比之下,也不难看出各自的缺点

1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。 
3、综上,如果要使用的命令支持一次处理多个文件, 那么使用 xargs比较方便; 
4、xargs不能操作文件名有空格的文件;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容