Find文件查找

在这里插入图片描述

本文来自我的博客地址

常用选项 (找文件)

语法格式

image-20231210194845588

选项参数表:

image-20231210194910732

image-20231210195003301

常用解释
-namefind /etc -name ‘*conf’
-inamefind /etc -name ‘*conf’ 忽略大小写
-userfind . -user username
-groupfind . -group groupname
-typefind . -type f
-sizefind /etc -size -10000c 查找小于10000字节的文件
-type 参数
-type 参数对应文件类型
f文件类型
d目录
c字符设备文件
b块设备文件
l链接文件
p管道文件
-size 参数
-size 参数说明
-nfind /etc -size -10000c 查找小于10000字节的文件
+nfind /etc -size +1M 查找大于10000字节的文件

c : 字节

k: 1000字节

M : Mb

-mtime

以天为单位

-mtime 参数说明
-nn 天内修改的文件
+nn 天以前修改的文件
n第 n 天修改的文件

示例

  1. 查找 /etc 下 5 天内修改的文件, 并以 conf 结尾
  2. 查找 /etc 10 天前修改的文件, 属主为 root
$ find /etc -mtime -5 -name '*conf'

$ find /etc -mtime +10 -user root
-mmin
-mmin 参数说明
-nn 分钟内修改
+nn 分钟以前修改
-mindepth

要作为 find 第一个选项

从第 n 级子目录开始搜索

要搜索的目录为第一个目录

-maxdepth

-newer

$ find /etc -newer 123.txt  # 查找比 123.txt 更新的文件

find 操作 (执行)

  • -print
  • -exec
  • -ok : 与 -exec 一致, 需要用户交互

-exec

删除

$ find ./etc -name "*conf" -exec rm -rf {} \;

复制

$ find ./etc -size +1M -exec cp {} ./test/ \;

练习: 把 /var/log/ 下 7 天以上的文件删除

$ find /var/log/ -name "*.log" -mtime +7 -exec rm -rf {} \;

逻辑运算符

  • -a 与
  • -o 或
  • -not | ! 非
$ find . -not -user <username> 
$ find . ! -user <username> 

$ find -type f -a -user <username> -a -size +300c

# 属主为 ? 或以 .yml 结尾的文件
$ find . -type f -a \( -user <username> -o -name '*.yml' \)

其他查找命令

locate: 默认部分匹配

位于软件包 mlocate

find 是直接从磁盘中查找, locate 是从数据库中查找

数据库文件位置: /var/lib/mlocate/mlocate.db -> 通过 updatedb 更新数据库文件

数据库配置文件: /etc/updatedb.conf

在后台 cron 定定时任务执行更新

whereis: 查找二进制文件

选项说明
-b二进制文件
-m返回帮助文档
-s返回源代码文件

which: 查找二进制程序文件

which mysql
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

M·K·T

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

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

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

打赏作者

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

抵扣说明:

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

余额充值