find 命令简单总结(1)

本文详细介绍Linux下find命令的使用方法,包括如何指定搜索目录、文件特征及处理动作等。通过多个示例展示如何查找特定类型的文件、按文件大小或修改时间筛选文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

find 可以算是 linux 非常重要的命令了,用法也非常复杂。
作为 Linux管理员 ,这个命令也是经常用到,所以就总结一下,同时也分享给一些刚刚学习Linux的朋友们。
用法介绍
find <指定目录> <指定条件> <指定动作>
  -<指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。
  - <指定条件>: 所要搜索的文件的特征。
  - <指定动作>: 对搜索结果进行特定的处理,显示到标准输出,默认为print

说一些 常用的选项
1 -name 可以指定 文件名称,进行查找。
查找当前目录(以及子目录) 以.txt 结尾的文件
find ./ -name "*.txt"
2   -type 可以指定文件类型
           f   文件            d  目录
           b  块设备           c   字符设备
           l    符合链接文件           p  命令管道文件           s  套接字文件
其中 f d 应该是用的比较多的。
查找 当前目录 下 的目录文件

当然也可以 换一种显示方式,通过xargs 这个 参数 4个换一行。(后面会详细说)

查看普通文件:
[root@myCentOS ~]# find /root/test2 -type f
/root/test2/3.txt
/root/test2/1.txt
/root/test2/2.txt
3 -mtime 通过 修改文件的修改时间来查找文件 ,是非常有用的。 格式有很多。
这种 格式 有几种 :(默认的单位是
-atime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
-ctime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
-mtime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
关于这几个时间 我就不多解释了, atime 访问时间,
ctime ,change time , 写入文件,修改权限,链接,
mtime , modified time 写入文件是随文件内容的更改而更改。
当然 也可以按照 分钟来查找
 -amin   +n/-n 时间为分钟,与atime 相对应
-mmin +n/-n
-cmin +n/-n
举个例子
查找当前目录 10天内 修改的文件
find . -mtime -10
find . -mtime -10
举个例子:
例1.查找 10 内没有修改的文件
[root@myCentOS ~]# find . -mtime -10|xargs -n 5
. ./Cprograme ./Cprograme/hello.i ./Cprograme/hello ./Cprograme/hello.c
./Cprograme/hello.s ./Cprograme/hello.o ./hello ./.lesshst ./hello.c
./8.txt ./shell ./shell/1.sh ./.bash_history ./.gitconfig
./.ssh ./.ssh/id_rsa ./.ssh/id_rsa.pub ./.ssh/known_hosts ./.viminfo
./finddir

例2 搜索当前目录中,所有过去10分钟中更新过的普通文件
cd finddir/
touch {1..10}.txt
[root@myCentOS finddir]# find . -mmin -10
.
./3.txt
./5.txt
./1.txt
./7.txt
./8.txt
./10.txt
./6.txt
./4.txt
./9.txt
./2.txt
例子3 用 ctime
现在 编辑 2.txt
find . -cmin -2 查找 2min 内 改变的 文件
[root@myCentOS finddir]# find . -cmin -2
.
./2.txt
4    -size
文件的大小, k M G
-size -10k 小于10 k 的文件
-size +10k 大于 10k 的文件
-size -1M 小于1M 的文件
-size +1M 大于1M 的文件
举例: 在 vim 1.txt 写点东西。
1) 查找当前目录下 大于10k 的文件。
[root@myCentOS finddir]# find . -size +10k
./1.txt
来看一下 这个文件 大小:
find . -size +10k | xargs ls -lh
-rw-r--r-- 1 root root 22K 9月 10 20:59 ./1.txt
[root@myCentOS finddir]# find . -size -10k
.
./3.txt
./5.txt
./7.txt
./8.txt
./10.txt
./6.txt
./4.txt
./9.txt
./2.txt
来看一下,ls 下的文件 大小
[root@myCentOS finddir]# ls -lh
总用量 28K
-rw-r--r-- 1 root root 0 9月 10 20:41 10.txt
-rw-r--r-- 1 root root 22K 9月 10 20:59 1.txt
-rw-r--r-- 1 root root 41 9月 10 20:50 2.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 3.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 4.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 5.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 6.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 7.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 8.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 9.txt
换一个目录 cd /root/
2) 查找 大于1M 的文件,
[root@myCentOS ~]# find . -size +1M
./backup/2.sql
[root@myCentOS ~]# ls -lh backup/2.sql
-rw-r--r-- 1 root root 2.3M 8月 26 21:52 backup/2.sql
5 -maxdepth 基于目的深度搜索,
现在 测试一下
比如说 -maxdepth 1 1表示只在当前目录查找, 2 表示向下两级
比如在当前目录下查找 txt 文件
[root@myCentOS ~]# find . -maxdepth 1 -name "*.txt" |xargs -n 4
./3.txt ./5.txt ./a.txt ./tmp.txt
./1.txt ./7.txt ./11.txt ./8.txt
./6.txt ./4.txt ./12.txt ./test.txt
./2.txt
mkdir -p 111/222/333/444/555
cd /root/111/222/333/444/555
vim 1.txt
cd /root/
find -maxdepth 6 -name "*.txt" |grep 1.txt


6 -iname 忽略大小写
用文件名查找文件,忽略大小写
[root@myCentOS finddir]# ls
10.txt 2.txt 4.txt 6.txt 8.txt test.txt
1.txt 3.txt 5.txt 7.txt 9.txt TEST.TXT
[root@myCentOS finddir]# find . -iname "test.txt"
./TEST.TXT
./test.txt
[root@myCentOS finddir]# find . -iname "TEST.txt"
./TEST.TXT
./test.txt
7 -a -o -not 条件限制
-a:表示且
-o:表示或
-not:表示非
举个例子
1)查找当前目录下 以.sql 结尾,且 文件大小大于2M的文件
[root@myCentOS ~]# find . -name "*.sql" -a -size +2M
./backup/2.sql
[root@myCentOS ~]# du -sh ./backup/2.sql
2.3M ./backup/2.sql
2) 查找当前目录下 以.sql 结尾,或者 大于2M 的文件
[root@myCentOS ~]# find . -name "*.sql" -o -size +2M
./12.sql
./1.sql
./backup/1.sql
./backup/4.sql
./backup/3.sql
./backup/2.sql
./11.sql
3)查找 /root/finddir 下 不以 txt 结尾的文件。
cd /root/finddir
[root@myCentOS finddir]# ls
10.txt 1.sh 2.txt 4.txt 6.txt 8.txt et nu TEST.TXT
1.c 1.txt 3.txt 5.txt 7.txt 9.txt test.txt
[root@myCentOS finddir]# find . -not -name "*.txt"
.
./TEST.TXT
./et nu
./1.c
./1.sh
当然 find 命令还有很多选项<指定条件>,这里只是介绍了比较常用的选项
find 的指定动作

find <指定目录> <指定条件> <指定动作>

find的处理动作可以是:
       -print   默认为输出
       -ls          显示查找到的文件的详细信息,相当于'ls -l'
-exec COMMAND {} \;
-ok COMMAND {} \;
       -exec    COMMAND {}  \;     其中COMMAND中有对查找到的文件进行操作时,用{}来替代查找到的文件,\;表示使用-exec的结束符,是固定格式
exec与ok的区别:ok会提供交互式,让你确认。而exec则不需要;

下面举个例子:
还是在finddir 下面:
1) 查找以.sh 结尾的文件,之后交给ls 命令, 详细打印 文件的属性。
[root@myCentOS finddir]# find -name "*.sh"
./1.sh
[root@myCentOS finddir]# find -name "*.sh" -ls
131167 4 -rw-r--r-- 1 root root 1 9月 10 22:32 ./1.sh

2) 来测试 -ok 来测试 是否询问删除。
[root@myCentOS data]# ls /root/finddir/data/
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
find /root/finddir/data/ -name "*.txt" -ok rm {} \;
< rm ... /root/finddir/data/3.txt > ? n
< rm ... /root/finddir/data/5.txt > ? n
< rm ... /root/finddir/data/1.txt > ? n
< rm ... /root/finddir/data/7.txt > ? n
< rm ... /root/finddir/data/8.txt > ? n
< rm ... /root/finddir/data/10.txt > ? y
< rm ... /root/finddir/data/6.txt > ? n
< rm ... /root/finddir/data/4.txt > ? n
< rm ... /root/finddir/data/9.txt > ? n
< rm ... /root/finddir/data/2.txt > ? n
上面 询问我是否 删除10.txt 我输入了 'y' 代表删除,ls 看看。
[root@myCentOS data]# ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt


3) -exec 来试一试会不会提示,
[root@myCentOS data]# find /root/finddir/data/ -name "*.txt" -exec rm {} \;
[root@myCentOS data]# ls
[root@myCentOS data]#
已经没有文件了, 并且没有提示, 所以这种也是比较危险的,谨慎操作吧。
总结: exec与ok的区别:ok会提供交互式,让你确认。而exec则不需要;

当然 find 如果 要和 -exec 或者 |xargs 功能就强大多了。
1)
find . -name "*.txt" -exec rm {} \;

当然 如果你不放心的话, 可以用 -ok
[root@myCentOS data]# find . -name "*.txt" -ok rm {} \;
< rm ... ./3.txt > ? y
< rm ... ./5.txt > ? y
< rm ... ./1.txt > ? y
< rm ... ./7.txt > ? n
< rm ... ./8.txt > ? n
< rm ... ./10.txt > ? n
< rm ... ./6.txt > ? n
< rm ... ./4.txt > ? n
< rm ... ./9.txt > ? n
< rm ... ./2.txt > ? n
[root@myCentOS data]# ls
10.txt 2.txt 4.txt 6.txt 7.txt 8.txt 9.txt
这样可以支持 删除前进行询问,相对来说好一点。 y /n
2)可以批量重命名
find . -name "*.txt" -exec mv {} {}.bak \;

find . -name "*.txt" -type f -ok mv {} {}.bak\;
< mv ... ./3.txt > ? n
< mv ... ./5.txt > ? n
< mv ... ./1.txt > ? n
< mv ... ./7.txt > ? n
< mv ... ./8.txt > ? n
< mv ... ./10.txt > ? n
< mv ... ./6.txt > ? y
< mv ... ./4.txt > ? n
< mv ... ./9.txt > ? n
< mv ... ./2.txt > ? n
当然 也可以用 xargs 实现上面的功能
1)批量删除
find . -name "*.txt" |xargs rm

2)批量重名名
find . -name "*.txt" |xargs -i mv {} {}.bak
find . -name "*.bak" |xargs -I{} mv {} {}.bak

分享快乐,留住感动。你的点赞,就是我写作的最大的动力.
--于 20170405 23:15 阿常呓语


### 如何使用 `find` 命令实现模糊搜索 在 Linux 中,`find` 命令支持多种方式来实现模糊搜索。以下是几种常见的方法: #### 方法一:使用通配符进行模糊匹配 可以通过 `-name` 或 `-iname` 参数配合通配符(如 `*`, `?`)来进行模糊搜索。 - **星号 (`*`) 的作用** 星号表示任意数量的字符(包括零个字符)。例如,要查找所有扩展名为 `.txt` 的文件,可以运行以下命令: ```bash find /path/to/search -name "*.txt" ``` - **问号 (`?`) 的作用** 问号表示单个字符的位置占位符。如果希望找到名字为两个字母且第二个字母是 `a` 的文件,可运行如下命令: ```bash find /path/to/search -name "?a" ``` 这些操作均区分大小写。如果不关心大小写,则可以用 `-iname` 替代 `-name`[^1]。 --- #### 方法二:利用正则表达式增强灵活性 当需要更复杂的模式匹配时,可以借助 `-regex` 和 `-iregex` 参数。这两个参数允许指定 POSIX 正则表达式作为匹配条件。 例如,假设想寻找以数字开头的所有文件名,可以这样操作: ```bash find /path/to/search -regextype posix-extended -regex ".*/[0-9].*" ``` 这里设置了正则类型的选项为 `posix-extended` 并定义了一个简单的正则表达式去匹配路径中的任何部分包含至少一位数的情况[^2]。 对于忽略大小写的场景,只需替换为 `-iregex` 即可完成相同的功能需求但不考虑字母大小差异的影响[^3]。 --- #### 示例代码展示 下面给出几个具体的例子以便更好地理解上述理论知识点的应用实践情况: ##### 按照名称的部分字符串查找(不分大小写) ```bash find . -type f -iname "*report*.pdf" ``` 此条指令会在当前目录及其子目录下寻找所有的 PDF 文件,并且它们的名字里含有 "report" 字样 (无论 Report 还是 REPORT 都会被选中)[^1]. ##### 结合多个标准一起筛选目标对象 有时候可能还需要加上额外约束条件比如限定修改日期范围内的某些特定种类文档: ```bash find ~/Documents -type f \( -name "*.doc*" -o -name "*.xls*" \) -mtime -7 ``` 上面这段脚本的作用是从用户的 Documents 主文件夹开始向下遍历每一个层级结构直到叶子节点为止;期间只保留那些要么属于 Word 文档(.doc*)系列要么 Excel 表格(.xls*)家族成员的同时最近七天之内被改动过的项目列表出来显示给终端用户查看[^2]. --- ### 总结说明 综上所述,通过合理运用 `find` 命令的不同参数组合形式能够轻松达成各种复杂程度各异的任务要求下的资源定位目的。无论是简单粗暴型还是精细雕琢派都可以从中挑选适合自己的解决方案加以实施应用起来解决实际遇到的各种难题挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值