Linux中find常用方法详解

在linux中,find的使用频率还是挺高的,因为确实够好用简单。

下面列举几种find的常用场景:

  • 在home目录下查找以txt结尾的文件;
[root@caotest0918 home]# ls
1.txt  2.txt  VMwareTools-10.1.15-6677369.tar.gz  vmware-tools-distrib
[root@caotest0918 home]# find /home -name '*.txt'
/home/vmware-tools-distrib/doc/open_source_licenses.txt
/home/1.txt
/home/2.txt

如果我只想查找/home目录下的文件,而不对home下更深层次的目录进行查找,该怎么办呢,这时候可以使用-maxdepth参数来定义查找的目录层级,可以看到-maxdepth必须放到-iname之前,否则会有告警,跟-maxdepth相对应的是-mindepth;

[root@caotest0918 home]# find /home -iname '*.a'
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libshadowfb.a
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libxaa.a
/home/5.a
[root@caotest0918 home]# find /home -iname '*.a' -maxdepth 1
find: 警告: 您在非选项参数 -iname 后定义了 -maxdepth 选项,但选项不是位置选项 (-maxdepth 影响在它之前或之后的指定的比较测试)。请在其它参数之前指定选项。

/home/5.a
[root@caotest0918 home]# find /home -name '*.a' -maxdepth 1
find: 警告: 您在非选项参数 -name 后定义了 -maxdepth 选项,但选项不是位置选项 (-maxdepth 影响在它之前或之后的指定的比较测试)。请在其它参数之前指定选项。

/home/5.a
[root@caotest0918 home]# find /home  -maxdepth 1 -name *.a
/home/5.a
[root@caotest0918 home]# find /home -maxdepth 1-iname '*.a'
find: 本应对 -maxdepth 使用一个十进制的正整数作为参数,但却使用了 ‘1-iname’
[root@caotest0918 home]# find /home -maxdepth 1 -iname '*.a'
/home/5.a

如果txt结尾的不是小写字母,而是TXT或者Txt怎么办呢,这时候需要使用iname选项来忽略大小写;

[root@caotest0918 home]# ls
1.txt  2.txt  3.TXT  4.Txt  VMwareTools-10.1.15-6677369.tar.gz  vmware-tools-distrib
[root@caotest0918 home]# find /home -iname '*.txt'
/home/vmware-tools-distrib/doc/open_source_licenses.txt
/home/1.txt
/home/2.txt
/home/3.TXT
/home/4.Txt
  • 如果find查找条件里包含多个条件,可以使用参数-o代表或的意思,条件需要括号扩起来,并且括号需要转义;

[root@caotest0918 home]# find /home \( -iname '*.txt' -o -iname '*.a' -o -iname '*.b' \)
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libshadowfb.a
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libxaa.a
/home/vmware-tools-distrib/doc/open_source_licenses.txt
/home/1.txt
/home/2.txt
/home/3.TXT
/home/4.Txt
/home/5.a
/home/6.b

       同时后面还可以固定查询的文件类型,比如 -type d代表查找目录,-type f代表查找文件;

  • 以时间为查询条件,根据文件状态更改时间,特指文件属性的修改 (ctime)、修改时间,特指文件内容的修改 (mtime) 或访问时间 (atime) 来执行时间过滤。 这些是在几天内,所以如果你想要更细粒度的控制,你可以表示为在几分钟内(分别是 cminmmin 和 amin)。 除非你确切地知道你想要的时间,否则你可能会在 + (大于)或 - (小于)的后面加上数字。
[root@caotest0918 home]# find /home \( -iname '*.txt' -o -iname '*.a' -o -iname '*.b' \) -mtime +7
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libshadowfb.a
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libxaa.a
/home/vmware-tools-distrib/doc/open_source_licenses.txt
[root@caotest0918 home]# find /home \( -iname '*.txt' -o -iname '*.a' -o -iname '*.b' \) -ctime +7
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libshadowfb.a
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libxaa.a
/home/vmware-tools-distrib/doc/open_source_licenses.txt
[root@caotest0918 home]# find /home \( -iname '*.txt' -o -iname '*.a' -o -iname '*.b' \) -atime +7
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libshadowfb.a
/home/vmware-tools-distrib/lib/configurator/XFree86-4/4.2.x/libxaa.a
/home/vmware-tools-distrib/doc/open_source_licenses.txt
  • 以文件大小的范围来find,比如你想查找/var/log下大于10M的文件都有哪些,可以使用-size参数,+表示大于,-表示小余;
[root@caotest0918 home]# dd if=/dev/zero of=/var/log/haha bs=1M count=13
记录了13+0 的读入
记录了13+0 的写出
13631488字节(14 MB)已复制,0.0188375 秒,724 MB/秒
[root@caotest0918 home]# find /var/log/ -size +10M
/var/log/haha

find根据条件查找到相关内容后该怎么办呢,后面我们可以对查找的内容采取的行动

 

find -exec 与 find | xargs 都可以对查找到的文件进行处理,但是还是有着本质区别的,exec需要对查找的结果一条一条执行,每执行一次产生一个进程,而xargs可以一次性执行,只产生一个进程。

[root@caotest0918 ~]# find /home -maxdepth 1 -iname '*.a' |xargs rm
[root@caotest0918 ~]# cd /home/
[root@caotest0918 home]# ls
1.txt  2.txt  3.TXT  4.Txt  6.b  VMwareTools-10.1.15-6677369.tar.gz  vmware-tools-distrib
[root@caotest0918 home]# find /home -maxdepth 1 -iname '*.b' -exec rm {} \;
[root@caotest0918 home]# ls
1.txt  2.txt  3.TXT  4.Txt  VMwareTools-10.1.15-6677369.tar.gz  vmware-tools-distrib

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值