find
find用法
在指定目录下查找文件
补充说明
find命令 用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
语法
find(选项)(参数)
选项
-exec find命令对匹配的文件执行该参数所给出的其他linux命令。相应命令的形式为' 命令 - and' {} \;,注意{ }和\;之间的空格。
-ok 和- exec的作用相同,只不过和会人交互而已,OK执行前会向你确认是不是要执行。
find命令主要参数:
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用了- depth选项,那么-prune选项将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, -n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime选项,但它们都和-mtime选项
相似,所以我们在这里只介绍-mtime选项。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
-size n[cwbkMG] : 文件大小 为 n 个由后缀决定的数据块。其中后缀为:
b: 代表 512 位元组的区块(如果用户没有指定后缀,则默认为 b)
c: 表示字节数
k: 表示 kilo bytes (1024字节)
w: 字 (2字节)
M:兆字节(1048576字节)
G: 千兆字节 (1073741824字节)
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-delete (删除)
-maxdepth 查找最大目录层数 如 1,即只查找一层目录
-iname 按名称搜索无论大小写
-fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件
/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount 在查找文件时不跨越文件系统mount点。
-follow(过时,新版使用-L)如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
-o 是或者的意思
-a 是而且的意思
-not 是相反的意思
-empty 搜索空文件或空目录
-xdev 确保find不回去遍历所有的文件系统,排除系统区域,只读源目录,可移动设备、/proc运行目录(linux系统)等类似位置。
参数
起始目录:查找文件的起始目录。
案例
find 从哪找 -name '找什么'
[root@localhost130 ~]# ls /home/yechangyao/yechangyao/
101.log 1.log 1.log.bak ansible_new.log test2
[root@localhost130 ~]# find / -name 'ansible_new'
[root@localhost130 ~]# find / -name 'ansible_new.log'
/home/yechangyao/yechangyao/ansible_new.log
---通配符* 模糊搜索
[root@localhost130 ~]# find / -name 'ansible_new*'
/home/yechangyao/yechangyao/ansible_new.log
[root@localhost130 ~]#
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt ttxt.txt Videos
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt ttxt.txt.org yechangyao
[root@localhost130 yechangyao]# find . -type f -name '*.org'
./ttxt.txt.org
---find命令 -delete选项用法
[root@localhost130 yechangyao]# find . -type f -name '*.org' --delete
find: unknown predicate `--delete'
[root@localhost130 yechangyao]# find . -type f -name '*.org' -delete
[root@localhost130 yechangyao]# ls
Centos-7.repo cout_test.txt Documents epel-7.repo hello.sh news.txt Public test001.tar test1108.txt test3 test.txt ttxt.txt yechangyao
core.10299 Desktop Downloads expr.sh Music Pictures Templates test002.tar.gz test2 testFor.sh text1108.txt Videos
[root@localhost130 yechangyao]#
实例
# 当前目录搜索所有文件,文件内容 包含 “140.206.111.111” 的内容
find . -type f -name "*" | xargs grep "140.206.111.111"
根据文件或者正则表达式进行匹配
列出当前目录及子目录下所有文件和文件夹
find .
在/home
目录下查找以.txt结尾的文件名
find /home -name "*.txt"
同上,但忽略大小写
find /home -iname "*.txt"
当前目录及子目录下查找所有以.txt和.pdf结尾的文件
find . \( -name "*.txt" -o -name "*.pdf" \)
或
find . -name "*.txt" -o -name "*.pdf"
匹配文件路径或者文件
find /usr/ -path "*local*"
基于正则表达式匹配文件路径
find . -regex ".*\(\.txt\|\.pdf\)$"
同上,但忽略大小写
find . -iregex ".*\(\.txt\|\.pdf\)$"
否定参数
找出/home下不是以.txt结尾的文件
find /home ! -name "*.txt"
根据文件类型进行搜索
find . -type 类型参数
类型参数列表:
- f 普通文件
- l 符号连接
- d 目录
- c 字符设备
- b 块设备
- s 套接字
- p Fifo
基于目录深度搜索
向下最大深度限制为3
find . -maxdepth 3 -type f
搜索出深度距离当前目录至少2个子目录的所有文件
find . -mindepth 2 -type f
根据文件时间戳进行搜索
find . -type f 时间戳
UNIX/Linux文件系统每个文件都有三种时间戳:
- 访问时间 (-atime/天,-amin/分钟):用户最近一次访问时间。
- 修改时间 (-mtime/天,-mmin/分钟):文件最后一次修改时间。
- 变化时间 (-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。
搜索最近七天内被访问过的所有文件
find . -type f -atime -7
搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
搜索超过七天内被访问过的所有文件
find . -type f -atime +7
搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
找出比file.log修改时间更长的所有文件
find . -type f -newer file.log
根据文件大小进行匹配
find . -type f -size 文件大小单元
文件大小单元:
- b —— 块(512字节)
- c —— 字节
- w —— 字(2字节)
- k —— 千字节
- M —— 兆字节
- G —— 吉字节
搜索大于10KB的文件
find . -type f -size +10k
搜索小于10KB的文件
find . -type f -size -10k
搜索等于10KB的文件
find . -type f -size 10k
删除匹配文件
删除当前目录下所有.txt文件
find . -type f -name "*.txt" -delete
根据文件权限/所有权进行匹配
当前目录下搜索出权限为777的文件
find . -type f -perm 777
找出当前目录下权限不是644的php文件
find . -type f -name "*.php" ! -perm 644
找出当前目录用户tom拥有的所有文件
find . -type f -user tom
找出当前目录用户组sunk拥有的所有文件
find . -type f -group sunk
借助-exec
选项与其他命令结合使用
找出当前目录下所有root的文件,并把所有权更改为用户tom
find .-type f -user root -exec chown tom {} \;
上例中, {} 用于与 -exec 选项结合使用来匹配所有文件,然后会被替换为相应的文件名。
找出自己家目录下所有的.txt文件并删除
find $HOME/. -name "*.txt" -ok rm {} \;
上例中, -ok 和 -exec 行为一样,不过它会给出提示,是否执行相应的操作。
查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> /all.txt
将30天前的.log文件移动到old目录中
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
因为单行命令中-exec参数中无法使用多个命令,以下方法可以实现在-exec之后接受多条命令
-exec ./text.sh {} \;
搜索但跳过指定的目录
查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
find . -path "./sk" -prune -o -name "*.txt" -print
⚠️ ./sk 不能写成 ./sk/ ,否则没有作用。
忽略两个目录
find . \( -path ./sk -o -path ./st \) -prune -o -name "*.txt" -print
⚠️ 如果写相对路径必须加上
./
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?