linux系统中find命令
linux系统中find命令
1、直接查找文件名
测试文件如下:
[root@centos79 test]# ls 01.txt 02.csv 02.txt 03.csv 04.txt [root@centos79 test]# find "02.txt" 02.txt [root@centos79 test]# find *.csv 02.csv 03.csv
2、find后加查找的路径、 使用-name参数指定文件名
[root@centos79 test]# ls 01.txt 02.csv 02.txt 03.csv 04.txt [root@centos79 test]# find ./ -name "03.csv" ./03.csv [root@centos79 test]# find ./ -name "*.txt" ./01.txt ./04.txt ./02.txt
指定目录:
[root@centos79 test]# ls 01.txt 02.csv 02.txt 03.csv 04.txt [root@centos79 test]# mkdir test01 test02 [root@centos79 test]# cp *.txt *.csv test01 [root@centos79 test]# cp *.txt *.csv test02 [root@centos79 test]# find ./test01/ -name "*.txt" ./test01/01.txt ./test01/02.txt ./test01/04.txt [root@centos79 test]# ls 01.txt 02.csv 02.txt 03.csv 04.txt test01 test02
3、查找时,使用-iname参数忽略文件名的大小写
[root@centos79 test]# ls 01.txt 01.TXT 02.csv 02.txt 03.csv 04.txt [root@centos79 test]# find ./ -name "01.txt" ./01.txt [root@centos79 test]# find ./ -iname "01.txt" ./01.txt ./01.TXT
4、-not参数或者 !反向查找
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt [root@centos79 test]# find ./ -name "01.txt" ./01.txt [root@centos79 test]# find ./ -not -name "01.txt" ./ ./04.txt ./03.csv ./02.csv [root@centos79 test]# find ./ ! -name "01.txt" ./ ./04.txt ./03.csv ./02.csv
继续:
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt [root@centos79 test]# find ./ -name "*.txt" ./01.txt ./04.txt [root@centos79 test]# find ./ -not -name "*.txt" ./ ./03.csv ./02.csv [root@centos79 test]# find ./ ! -name "*.txt" ./ ./03.csv ./02.csv
5、使用-maxdepth和-mindepth限定查找的深度
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt test1 [root@centos79 test]# tree . ├── 01.txt ├── 02.csv ├── 03.csv ├── 04.txt └── test1 ├── 01.txt ├── 02.csv └── test2 ├── 01.txt ├── 02.csv └── test3 ├── 01.txt ├── 02.csv └── test4 ├── 01.txt └── 02.csv 4 directories, 12 files [root@centos79 test]# find ./ -maxdepth 1 -name "*.txt" ./01.txt ./04.txt [root@centos79 test]# find ./ -maxdepth 2 -name "*.txt" ./01.txt ./04.txt ./test1/01.txt [root@centos79 test]# find ./ -maxdepth 3 -name "*.txt" ./01.txt ./04.txt ./test1/test2/01.txt ./test1/01.txt
继续
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt test1 [root@centos79 test]# tree . ├── 01.txt ├── 02.csv ├── 03.csv ├── 04.txt └── test1 ├── 01.txt ├── 02.csv └── test2 ├── 01.txt ├── 02.csv └── test3 ├── 01.txt ├── 02.csv └── test4 ├── 01.txt └── 02.csv 4 directories, 12 files [root@centos79 test]# find ./ -mindepth 2 -name "*.txt" ./test1/test2/test3/test4/01.txt ./test1/test2/test3/01.txt ./test1/test2/01.txt ./test1/01.txt [root@centos79 test]# find ./ -mindepth 3 -name "*.txt" ./test1/test2/test3/test4/01.txt ./test1/test2/test3/01.txt ./test1/test2/01.txt
6、同时满足两个条件查找
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt [root@centos79 test]# find ./ -name "*.txt" ./01.txt ./04.txt [root@centos79 test]# find ./ -name "*.txt" -not -name "*4*" ./01.txt
7、-o参数同时对多种条件查找
[root@centos79 test]# ls 01.txt 02.csv 03.csv 04.txt 05.map 06.map [root@centos79 test]# find ./ -name "*.txt" -name "*.map" [root@centos79 test]# find ./ -name "*.txt" -o -name "*.map" ./01.txt ./04.txt ./05.map ./06.map
8、限定文件或者目录进行查找
[root@centos79 test]# ls test1 test2 test3 test4 [root@centos79 test]# ll -h total 0 -rw-r--r--. 1 root root 0 Jul 6 23:23 test1 -rw-r--r--. 1 root root 0 Jul 6 23:23 test2 drwxr-xr-x. 2 root root 6 Jul 6 23:23 test3 drwxr-xr-x. 2 root root 6 Jul 6 23:23 test4 [root@centos79 test]# find ./ -name "test*" ./test1 ./test2 ./test3 ./test4 [root@centos79 test]# find ./ -type f -name "test*" ./test1 ./test2 [root@centos79 test]# find ./ -type d -name "test*" ./test3 ./test4
9、同时对多个目录进行查找
[root@centos79 test]# ls a.txt b.csv test1 test2 test3 [root@centos79 test]# tree . ├── a.txt ├── b.csv ├── test1 │ ├── a.txt │ └── b.csv ├── test2 │ ├── a.txt │ └── b.csv └── test3 ├── a.txt └── b.csv 3 directories, 8 files [root@centos79 test]# find ./ -name "*.txt" ./a.txt ./test1/a.txt ./test2/a.txt ./test3/a.txt [root@centos79 test]# find ./test1/ -name "*.txt" ./test1/a.txt [root@centos79 test]# find ./test1/ ./test3/ -name "*.txt" ./test1/a.txt ./test3/a.txt
10、继续文件(目录)权限进行查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rw-r--r--. 1 root root 0 Jul 6 23:28 a.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# chmod u+x a.txt [root@centos79 test]# chmod 777 b.txt [root@centos79 test]# chmod o+w d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 root root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 root root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# find ./ -perm 777 ./b.txt [root@centos79 test]# find ./ -perm 744 ./a.txt
继续:
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 root root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 root root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# find ./ -not -perm 777 ./ ./a.txt ./c.txt ./d.txt [root@centos79 test]# find ./ -perm /o=x ./ ./b.txt [root@centos79 test]# find ./ -perm /o=w ./b.txt ./d.txt [root@centos79 test]# find ./ -perm /u=x ./ ./a.txt ./b.txt
11、基于所属的用户进行查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 root root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 root root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# chown liujiaxin01 a.txt b.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# find ./ -user root ./ ./c.txt ./d.txt [root@centos79 test]# find ./ -user liujiaxin01 ./a.txt ./b.txt
12、根据所属的组进行查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root root 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root root 0 Jul 6 23:28 d.txt [root@centos79 test]# chgrp liujiaxin01 c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root liujiaxin01 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root liujiaxin01 0 Jul 6 23:28 d.txt [root@centos79 test]# find ./ -group liujiaxin01 ./c.txt ./d.txt [root@centos79 test]# find ./ -group root ./ ./a.txt ./b.txt
13、根据修改权限时间查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root liujiaxin01 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root liujiaxin01 0 Jul 6 23:28 d.txt [root@centos79 test]# date Tue Jul 6 23:51:10 CST 2021 [root@centos79 test]# find ./ -cmin -5 [root@centos79 test]# find ./ -cmin -50 ./ ./a.txt ./b.txt ./c.txt ./d.txt
14、跟去过去内容被修改的时间进行查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root liujiaxin01 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root liujiaxin01 0 Jul 6 23:28 d.txt [root@centos79 test]# date Tue Jul 6 23:52:50 CST 2021 [root@centos79 test]# find ./ -mmin -5 [root@centos79 test]# find ./ -mmin -50 ./ ./a.txt ./b.txt ./c.txt ./d.txt
15、根据过去时间段被访问的时间进行查找
[root@centos79 test]# ls a.txt b.txt c.txt d.txt [root@centos79 test]# ll -h total 0 -rwxr--r--. 1 liujiaxin01 root 0 Jul 6 23:28 a.txt -rwxrwxrwx. 1 liujiaxin01 root 0 Jul 6 23:28 b.txt -rw-r--r--. 1 root liujiaxin01 0 Jul 6 23:28 c.txt -rw-r--rw-. 1 root liujiaxin01 0 Jul 6 23:28 d.txt [root@centos79 test]# date Tue Jul 6 23:54:06 CST 2021 [root@centos79 test]# find ./ -amin -6 [root@centos79 test]# find ./ -amin -60 ./ ./a.txt ./b.txt ./c.txt ./d.txt
16、根据文件的大小进行查找
[root@centos79 test]# ll -h total 1.1G -rw-r--r--. 1 root root 1.0M Jul 6 23:55 a.txt -rw-r--r--. 1 root root 10M Jul 6 23:56 b.txt -rw-r--r--. 1 root root 100M Jul 6 23:56 c.txt -rw-r--r--. 1 root root 1000M Jul 6 23:56 d.txt [root@centos79 test]# find ./ -size +100M ./d.txt [root@centos79 test]# find ./ -size -100M ./ ./a.txt ./b.txt [root@centos79 test]# find ./ -size +1M -size -1000M ./b.txt ./c.txt
17、查找最大文件
[root@centos79 test]# ls a.txt b.txt c.txt d.txt e.txt [root@centos79 test]# ll -h total 1.2G -rw-r--r--. 1 root root 1000M Jul 6 23:59 a.txt -rw-r--r--. 1 root root 10M Jul 6 23:56 b.txt -rw-r--r--. 1 root root 100M Jul 6 23:56 c.txt -rw-r--r--. 1 root root 1.0M Jul 6 23:55 d.txt -rw-r--r--. 1 root root 50M Jul 7 00:03 e.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; 10240 ./b.txt 102400 ./c.txt 1024000 ./a.txt 1024 ./d.txt 51200 ./e.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; | sort -n 1024 ./d.txt 10240 ./b.txt 51200 ./e.txt 102400 ./c.txt 1024000 ./a.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; | sort -n -r 1024000 ./a.txt 102400 ./c.txt 51200 ./e.txt 10240 ./b.txt 1024 ./d.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; | sort -n -r | head -n 1 1024000 ./a.txt
18、查找最小文件
[root@centos79 test]# ls a.txt b.txt c.txt d.txt e.txt [root@centos79 test]# ll -h total 1.2G -rw-r--r--. 1 root root 1000M Jul 6 23:59 a.txt -rw-r--r--. 1 root root 10M Jul 6 23:56 b.txt -rw-r--r--. 1 root root 100M Jul 6 23:56 c.txt -rw-r--r--. 1 root root 1.0M Jul 6 23:55 d.txt -rw-r--r--. 1 root root 50M Jul 7 00:03 e.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; 10240 ./b.txt 102400 ./c.txt 1024000 ./a.txt 1024 ./d.txt 51200 ./e.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; | sort -n 1024 ./d.txt 10240 ./b.txt 51200 ./e.txt 102400 ./c.txt 1024000 ./a.txt [root@centos79 test]# find ./ -type f -exec ls -s {} \; | sort -n | head -n 1 1024 ./d.txt
19、查找空文件和空目录
[root@centos79 test]# ls a.txt b.txt c.txt test01 test02 [root@centos79 test]# ll -h total 1010M -rw-r--r--. 1 root root 1000M Jul 6 23:59 a.txt -rw-r--r--. 1 root root 10M Jul 6 23:56 b.txt -rw-r--r--. 1 root root 0 Jul 7 00:08 c.txt drwxr-xr-x. 2 root root 6 Jul 7 00:09 test01 drwxr-xr-x. 2 root root 19 Jul 7 00:09 test02 [root@centos79 test]# tree . ├── a.txt ├── b.txt ├── c.txt ├── test01 └── test02 └── b.txt 2 directories, 4 files [root@centos79 test]# find ./ -type f -empty ./c.txt [root@centos79 test]# find ./ -type d -empty ./test01
20、对查找到的文件进行操作
[root@centos79 test]# ls a.txt b.txt c.csv e.csv [root@centos79 test]# find ./ -name "*.txt" -exec ls -lh {} \; -rw-r--r--. 1 root root 10M Jul 6 23:56 ./b.txt -rw-r--r--. 1 root root 1000M Jul 6 23:59 ./a.txt
继续
[root@centos79 test]# ls a.txt b.txt c.csv e.csv [root@centos79 test]# ls ~/Downloads/ [root@centos79 test]# find ./ -name "*.csv" -exec cp {} ~/Downloads/ \; [root@centos79 test]# ls ~/Downloads/ c.csv e.csv
继续
[root@centos79 test]# ls a.txt b.txt c.csv e.csv [root@centos79 test]# find ./ -type f -name "*.txt" -exec rm -f {} \; [root@centos79 test]# ls c.csv e.csv
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律