Find 查找命令

精选 Find 查找命令 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#💡 选项参数示例

选项参数 常用示例 功能说明
-type find . -type d 仅查找目录 (Directory)
-name find . -type f -name "*.txt" 按名称精确/通配符查找文件
-iname find . -type f -iname "hello" 按名称查找文件(不区分大小写)
-size find . -size +1G 查找大于 1GB 的文件
-user find . -type d -user jack 查找属于指定用户 (jack) 的文件/目录
-regex find /var -regex '.*/tmp/.*[0-9]*.file' 使用正则表达式查找,参考 正则速查
-maxdepth find . -maxdepth 1 -name "a.txt" 限制最大搜索深度(此处仅在当前层级查找)
-mindepth find / -mindepth 3 -maxdepth 5 -name pass 限制最小搜索深度在第 3 至第 5 层子目录之间

#📂 文件类型匹配 (-type)

参数标志 对应文件类型
-type d 目录 (Directory)
-type f 普通文件 (File)
-type l 符号链接 (Symbolic link)
-type b 块设备文件 (Buffered block)
-type c 字符设备文件 (Unbuffered character)
-type p 命名管道 (Named pipe)
-type s 套接字文件 (Socket)

#📏 文件大小单位 (-size)

单位符号 含义描述
-size b 512 字节块(默认单位)
-size c 字节 (Bytes)
-size k KB (Kilobytes)
-size M MB (Megabytes)
-size G GB (Gigabytes)
-size T TB (Terabytes) (仅 BSD 支持)
-size P PB (Petabytes) (仅 BSD 支持)

#⚖️ 按文件大小筛选 (+ / -)

查找所有大于 10MB 的文件

$ find / -size +10M

查找所有小于 10MB 的文件

$ find / -size -10M

查找正好等于 10MB 的文件

$ find / -size 10M

查找大小在 100MB 至 1GB 之间的文件

$ find / -size +100M -size -1G

前缀 + 代表大于,- 代表小于。

#🏷️ 按文件名与后缀查找

在当前目录下按文件名查找

$ find . -name tecmint.txt

在指定目录 (/home) 下查找文件

$ find /home -name tecmint.txt

忽略文件名大小写查找

$ find /home -iname tecmint.txt

按名称查找指定目录

$ find / -type d -name tecmint

查找特定名称的 PHP 文件

$ find . -type f -name tecmint.php

查找当前目录下所有 .php 后缀文件

$ find . -type f -name "*.php"

#🔒 按文件权限查找 (-perm)

查找权限为 777 的文件

$ find . -type f -perm 0777 -print

查找权限不是 777 的文件

$ find / -type f ! -perm 777

查找设置了 SUID 权限的文件

$ find / -perm /u=s

查找设置了 SGID 权限的文件

$ find / -perm /g=s

查找可读 (Read Only) 的文件

$ find / -perm /u=r

查找可执行 (Executable) 的文件

$ find / -perm /a=x

#👤 按所有者与用户组查找

按用户名查找指定文件

$ find / -user root -name tecmint.txt

查找指定用户的所有文件

$ find /home -user tecmint

查找指定用户组的所有文件

$ find /home -group developer

查找指定用户下后缀为 .txt 的文件

$ find /home -user tecmint -iname "*.txt"

#🔀 匹配多种文件名组合

$ find . -type f \( -name "*.sh" -o -name "*.txt" \)

同时查找扩展名为 .sh.txt 的文件

#📁 在多个路径下同时查找

$ find /opt /usr /var -name foo.scala -type f

同时在 /opt/usr/var 多个目录下查找指定文件

#🈳 查找空文件与空目录

查找当前目录下的所有空目录

$ find . -type d -empty

删除当前目录下的所有空文件

$ find . -type f -empty -delete

#📅 按时间维度查找

#⏱️ 时间维度区别说明

选项参数 含义说明
atime 访问时间 (access time,文件上次被打开查看的时间)
mtime 修改时间 (modified time,文件内容上次被改变的时间)
ctime 状态改变时间 (changed time,文件元数据/inode 上次改变的时间)

时间筛选选项速查

选项参数 描述说明
-mtime +0 修改时间大于 24 小时之前
-mtime 0 修改时间在过去 24 小时之内
-mtime -1 修改时间小于 1 天之前(等同于 -mtime 0
-mtime 1 修改时间在 24 至 48 小时之间
-mtime +1 修改时间在 48 小时之前
-mtime +1w 上次修改时间在 1 周以前
-atime 0 上次访问时间在过去 24 小时之内
-atime +0 上次访问时间在 24 小时之前
-atime 1 上次访问时间在 24 至 48 小时之间
-atime +1 上次访问时间在 48 小时之前
-atime -1 上次访问时间在 24 小时之内
-ctime -6h30m 文件状态在最近 6 小时 30 分钟内发生改变

#💡 按时间查找示例

查找在过去 50 天被修改过的文件

$ find / -mtime 50

查找在过去 50 天被访问过的文件

$ find / -atime 50

查找在 50 至 100 天之间被修改过的文件

$ find / -mtime +50 –mtime -100

查找在过去 1 小时内状态发生改变的文件

$ find / -cmin -60

查找在过去 1 小时内内容被修改过的文件

$ find / -mmin -60

查找在过去 1 小时内被访问过的文件

$ find / -amin -60

#⚡ 查找与组合操作 (Find and Exec)

#🗑️ 查找并删除 (Delete)

查找并删除所有 MP3 音频文件

$ find . -type f -name "*.mp3" -exec rm -f {} \;

查找并删除指定名称的文件

$ find . -type f -name "tecmint.txt" -exec rm -f {} \;

查找并删除大于 100MB 的文件

$ find / -type f -size +100m -exec rm -f {} \;

查找大于 10MB 的 MP3 文件并直接删除

$ find / -type f -name *.mp3 -size +10m -exec rm {} \;

#✏️ 查找并内容替换 (Replace)

查找所有文件并将文件中的 const 替换为 let

$ find ./ -type f -exec sed -i 's/const/let/g' {} \;

查找可读可写的文件并将 old 替换为 new

$ find ./ -type f -readable -writable -exec sed -i "s/old/new/g" {} \;

参考:Sed 命令备忘单

#🏷️ 查找并批量重命名 (Rename)

查找文件并批量添加 .bak 备份后缀

$ find . -type f -name 'file*' -exec mv {} {}.bak\;

查找文件并批量修改文件扩展名 (.html => .gohtml)

$ find ./ -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.gohtml"' _ {} \;

#🚚 查找并移动 (Move)

$ find . -name '*.mp3' -exec mv {} /tmp/music \;

查找所有 .mp3 文件并移动到指定目录 (/tmp/music)

#📋 查找并复制 (Copy)

$ find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \;

查找符合条件的文件并复制到备份目录 (/tmp/backup)

#🔗 查找并合并文本 (Concatenate)

将下载目录下的所有 CSV 文件合并导出为 merged.csv

$ find download -type f -iname '*.csv' | xargs cat > merged.csv

将下载目录下排序后的 CSV 文件合并导出为 merged.csv

$ find download -type f -iname '*.csv' | sort | xargs cat > merged.csv

#📊 查找并排序 (Sort)

查找文件并按升序排列输出

$ find . -type f | sort

查找文件并按降序排列输出

$ find . -type f | sort -r

#🔐 查找并修改权限 (Chmod)

查找所有文件并将权限批量设置为 644

$ find / -type f -perm 0777 -print -exec chmod 644 {} \;

查找所有目录并将权限批量设置为 755

$ find / -type d -perm 777 -print -exec chmod 755 {} \;

#📦 查找并压缩 (Compress)

查找所有 .java 文件并打包压缩为 java.tar

$ find . -type f -name "*.java" | xargs tar cvf java.tar

查找所有 .csv 文件并压缩打包为 ZIP 文件

$ find . -type f -name "*.csv" | xargs zip data.zip