find的基本用法

find作用

根据预设的条件递归查询对应的文件
-find [目录] [条件1] [-a|-o][条件2]…
-常用条件表示

find的用法

①-type 类型(f、d、l、b、c、p)文件f,目录d,符号链接文件l,块设备文件b,字符设备文件c,管道文件p
②-name 或 iname 文档名称
③-size +|- 文件大小(K、M、G)
④-user 用户名
⑤-group 组名
⑥-mtime 修改时间
⑦-maxdepth 查询深度
⑧查找文件使用find 命令的-exec操作

举个例子:

①-type 类型

find /boot/ -type f      //查找文本文件
find /boot/ -type d      //查找目录
find /boot/ -type l      //查找快捷方式

②-name 文档名称

find /etc -name "passwd"    //查找名为passwd的文档,包括(文件,目录,快捷方式)
find /etc  -name  "*.conf"    // 查找/etc以.conf结尾的文档(不包含子目录)
find /etc/ -name "passwd" -type f       //查找/etc下名为passwd的文件
find /etc/ -name "passwd"  -o -type f    //查找/etc/下名为passwd的文档,或文件(-o选项满足其中一个即可
find /var/   -name    "ftp"  -type d     //查找/var名为ftp的目录
find /etc/ -name "PASSWD"        //不忽略大小写查询
find /etc/ -iname "PASSWD"      // 忽略大小写查询

③-size +10M 文件大小 (R、M、G)

find /boot/ -size +10M  //查找/boot下大于10M的文件
find /boot/ -size -10M  //查找/boot小于10M的文件

④-user 用户

find / -user student //查找/下属于student用户的文件

⑤-group 组名

find /home/ -group  //查找/home下属于student组的文件

⑥-mtime 根据文件修改时间(都是过去时间)

mtime +10 过去10天之后
-mtime -10 最近10天之内

find /var/log/ -mtime -10  -type f     //查询/var/log最近10天之内的日志文件
find /var/log/ -mtime +10 -type f |wc -l   //查询/var/log过去10天之后的日志文件并统计日志文件个数

⑦-maxdepth 限制目录查询的深度(最大层数)

find /etc/ -maxdepth 1 -name "*.conf"       //限制最大查询层数为1
find /etc/ -maxdepth 2 -name "*.conf"       //限制最大查询层数为2
find /etc/ -maxdepth 3 -name "*.conf"       //限制最大查询层数为3

⑧find 命令的-exec操作

find … … -exec 处理命令 {};
-优势:以{}代替每一个结果,逐个处理,遇;结束

find /tmp/ -name "hello"  -type f  -exec cp {} /opt \;   //查询/tmp下名为hello的文件并拷贝到/opt目录下    
find /boot/ -size +10M -type f -exec cp {} /opt \;     //查询/boot下大于10M的文件并拷贝到/opt目录下
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜小徐呐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值