【Shell】find文件查找

语法格式

find [路径] [选项] [操作]

选项参数对照表

在这里插入图片描述
在这里插入图片描述

常用选项

-name 查找/etc目录下以conf结尾的文件ind /etc -nam
-iname 查找当前目录下文件名为aa的文件,不区分大小写 find . -iname aa
-user 查找文件属主为hdfs的所有文件find . -user hdfs
-group 查找文件属组为yarn的所有文件find . -group yarn
-type

	f		文件			 		find . -type f
	d		目录					find . -type d
	c		字符设备文件			find . -type c
	b		块设备文件			find . -type b
	l		链接文件				find . -type l
	p		管道文件				find . -type p

-size 文件大小

    -n		小于n的文件
	+n	    大于n的文件
	 n		等于n的文件

例子1:查找/etc目录下小于10000字节的文件 find /etc -size -10000c
例子2:查找/etc目录下大于1M的文件 find /etc -size +1M

-mtime 修改天数

	-n 		n天以内修改的文件
	+n		n天以外修改的文件
	 n		正好n天修改的文件

例子1:查找/etc目录下5天之内修改且以cons结尾的文件 find /etc -mtime -5 -name ‘*.conf’
例子2:查找/etc目录下10天之前修改且属主为root的文件find /etc -mtime +10 -user root

-mmin 修改分钟

	-n		n分钟以内修改的文件
	+n		n分钟以外修改的文件

例子1:查找/etc目录下30分钟之前修改的文件 find /etc -mmin +30
例子2:查找/etc目录下30分钟之内修改的目录 find /etc -mmin -30 -type d

-mindepth n 表示从n级子目录开始搜索

例子:在/etc下的3级子目录开始搜索find /etc -mindepth 3

-maxdepth n 表示最多搜索到n-1级子目录

例子1:在/etc下搜索符合条件的文件,但最多搜索到2级子目录
find /etc -maxdepth 3 -name '*.conf'
例子2:find ./etc/ -type f -name '*.conf' -size +10k -maxdepth 2

不常用选项

-nouser 查询没有所属用户的文件,find /etc -type f -nouser
-nogroup 查询没有所属组的文件,find /etc -type f -nogroup
-perm 根据文件权限查找,find . -perm 664
-prune 排除文件或目录
通常和-path一起使用,用于将特定目录排除在搜索条件之外。-path ./etc -prune 固定格式。

例子1:查找当前目录下所有普通文件,但排除test自
find . -path ./etc -prune -o -type f
例子2:查找当前目录下所有普通文件,但排除etc和opt目录
find . -path ./etc -prune -o -path ./opt -prune -o -type f
例子3:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs
例子4:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs,且文件大小必须大于500字节
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs -a -size +500c

-newer file1 查找当前目录下比 a 文件要新的文件

例子: find /etc -newer a

操作

-print 打印输出。 默认的选项,即打印出找到的结果。
-exec 对搜索到的文件执行特定的操作,固定的格式为:-exec 'commond' {} \; 注意:{} 表示查询的结果。

例1: 搜索 /etc 目录下的文件(非目录),文件以 conf 结尾,且大于 10k,然后将其删除。
find /etc -type f -name '*.conf' -size +10k -exec rm -f {} \;
例2: 将 /data/log/ 目录下以 .log 结尾的文件,且更改时间在 7 天以上的删除。
find /data/log -name '*.log' -mtime +7 -exec rm -f \;
例3: 搜索条件同 例1 一样,但是不删除,只是将其复制到 /root/conf 目录下
find /etc -type f -name '*.conf' -size +10k -exec cp {} /root/conf/ \;

-ok 和 -exec 的功能一样,只是每次操作都会给用户提示。

逻辑运算符

-a    与 (默认情况查询条件之间都是 与 的关系)

-o    或

-not | !  非

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小叶柏杉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值