linux 中find命令的用法
1、在当前路径直接查找
root@ubuntu01:/home/test# ls 01.txt 02.csv 02.txt 03.csv 04.txt root@ubuntu01:/home/test# find 02.csv 02.csv root@ubuntu01:/home/test# find *.csv 02.csv 03.csv root@ubuntu01:/home/test# find *.txt 01.txt 02.txt 04.txt
2、指定路径,依据文件名进行查找(-or等价于-o)
root@ubuntu01:/home/test# ls 01.txt 02.csv 02.txt 03.csv 04.txt a.ped b.ped root@ubuntu01:/home/test# find ./ -name "*.txt" ## 查找当前路径下所有的txt文件 ./02.txt ./04.txt ./01.txt root@ubuntu01:/home/test# find ./ -name "*.txt" -or -name "*.ped" ## 查找当前路径下的所有txt文件和ped文件 ./b.ped ./02.txt ./04.txt ./01.txt ./a.ped root@ubuntu01:/home/test# find ./ -name "*.txt" -or -name "*.ped" -or -name "02.csv" ## 查找当前路径下所有的txt文件、ped文件和02.csv文件。 ./b.ped ./02.txt ./04.txt ./01.txt ./02.csv ./a.ped
3、反向查找(-not 等价于!)
root@ubuntu01:/home/test# ls 01.txt 02.csv 02.txt 03.csv 04.txt a.ped b.ped root@ubuntu01:/home/test# find ./ -not -name "*.txt" ## 查看名字不是.txt后缀的所有文件和目录 ./ ./b.ped ./03.csv ./02.csv ./a.ped root@ubuntu01:/home/test# find ./ -type f -not -name "*.txt" ## -type f限定查找的结果为文件,而不包含目录 ./b.ped ./03.csv ./02.csv ./a.ped root@ubuntu01:/home/test# find ./ -type f -not -name "*.txt" -not -name "*.csv" ## 查找除txt文件和csv文件以外的所有文件 ./b.ped ./a.ped
4、依据文件大小进行查找
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h ## 列出所有文件的大小 total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rw-r--r-- 1 root root 10M 3月 27 17:31 b.txt -rw-r--r-- 1 root root 100M 3月 27 17:32 c.txt -rw-r--r-- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-- 1 root root 150M 3月 27 17:32 e.txt
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rw-r--r-- 1 root root 10M 3月 27 17:31 b.txt -rw-r--r-- 1 root root 100M 3月 27 17:32 c.txt -rw-r--r-- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-- 1 root root 150M 3月 27 17:32 e.txt root@ubuntu01:/home/test# find ./ -type f -size 100M ## 查找大小为100M的文件 ./c.txt root@ubuntu01:/home/test# find ./ -type f -size +100M ## 查找大小大于100M的文件 ./e.txt ./d.txt root@ubuntu01:/home/test# find ./ -type f -size -100M ## 查找大小小于100M的文件 ./a.txt ./b.txt root@ubuntu01:/home/test# find ./ -type f -size +50M -size -200M ## 查找文件大小在50M到200M的文件 ./e.txt ./c.txt root@ubuntu01:/home/test# find ./ -type f -not -size +50M -or -not -size -200M ## 查找大小小于50M或者大于200M的文件 ./d.txt ./a.txt ./b.txt
root@ubuntu01:/home/test# find ./ -type f -size -50M -or -size +200M ## 查找大小小于50M或者大于200M的文件
./d.txt ./a.txt ./b.txt
5、依据文件的权限进行查找
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h ## 列出文件的权限 total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root root 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt*
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root root 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -perm 777 ## 查找文件权限为777的文件 ./b.txt root@ubuntu01:/home/test# find ./ -type f -perm 645 ## 查找文件权限为645的文件 ./e.txt
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root root 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -not -perm 777 ## 查找权限为非777的文件 ./e.txt ./d.txt ./a.txt ./c.txt
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root root 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -perm /o=x ## 查找其他人有执行权限的文件 ./e.txt ./b.txt root@ubuntu01:/home/test# find ./ -type f -perm /o=x -perm /g=w ## 查找其他人有执行权限、且所属组具有写入权限的文件 ./b.txt
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root root 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 root root 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -not -perm /o=x ## 查找其他人没有执行权限的文件 ./d.txt ./a.txt ./c.txt
6、依据所有者、所属组进行查找
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h ## 列出所有者、所属组 total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 liujiaxin01 root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root liujiaxin01 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 liujiaxin01 liujiaxin01 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt*
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h ## 列出权限 total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 liujiaxin01 root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root liujiaxin01 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 liujiaxin01 liujiaxin01 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -user root ## 查找所有者为root的所有文件 ./e.txt ./c.txt ./b.txt root@ubuntu01:/home/test# find ./ -type f -user root -group root ## 查找所有者、所属组同时为root的所有文件 ./e.txt ./b.txt
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 781M drwxr-xr-x 2 root root 4.0K 3月 27 17:32 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 liujiaxin01 root 20M 3月 27 17:31 a.txt -rwxrwxrwx 1 root root 10M 3月 27 17:31 b.txt* -rwxr--r-- 1 root liujiaxin01 100M 3月 27 17:32 c.txt* -rw-rw-rw- 1 liujiaxin01 liujiaxin01 500M 3月 27 17:32 d.txt -rw-r--r-x 1 root root 150M 3月 27 17:32 e.txt* root@ubuntu01:/home/test# find ./ -type f -group root ## 查找所属组为root的文件 ./e.txt ./a.txt ./b.txt root@ubuntu01:/home/test# find ./ -type f -group root -user liujiaxin01 ## 查找所属组为root、同时所有者为liujiaxin01的文件 ./a.txt
7、依据文件修改的时间进行查找
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# ll -h ## 列出文件创建的时间 total 16K drwxr-xr-x 2 root root 4.0K 3月 27 18:11 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 21 3月 27 18:10 a.txt -rw-r--r-- 1 root root 21 3月 27 14:37 b.txt root@ubuntu01:/home/test# date ## 查看系统时间 2022年 03月 27日 星期日 18:11:29 CST root@ubuntu01:/home/test# find ./ -type f -mmin -5 ## 查找过去5分钟内修改过的文件 ./a.txt root@ubuntu01:/home/test# find ./ -type f -mtime -5 ## 查找过去5天内修改过的文件 ./a.txt ./b.txt
8、查找空文件
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt f.txt root@ubuntu01:/home/test# ll -h total 84M drwxr-xr-x 2 root root 4.0K 3月 27 18:18 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 18:15 a.txt -rw-r--r-- 1 root root 0 3月 27 18:18 b.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:15 c.txt -rw-r--r-- 1 root root 50M 3月 27 18:15 d.txt -rw-r--r-- 1 root root 8.0M 3月 27 18:16 e.txt -rw-r--r-- 1 root root 0 3月 27 18:18 f.txt root@ubuntu01:/home/test# find ./ -type f -empty ## 查找空文件 ./b.txt ./f.txt
9、对查找的文件进行操作
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 84M drwxr-xr-x 2 root root 4.0K 3月 27 18:20 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 20M 3月 27 18:15 a.txt -rw-r--r-- 1 root root 0 3月 27 18:18 b.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:15 c.txt -rw-r--r-- 1 root root 50M 3月 27 18:15 d.txt -rw-r--r-- 1 root root 8.0M 3月 27 18:16 e.txt root@ubuntu01:/home/test# find ./ -type f -size +10M -exec rm -f {} \; ## 查找大小大于10M的文件,并进行删除 root@ubuntu01:/home/test# ls b.txt c.txt e.txt
root@ubuntu01:/home/test# ls b.txt c.txt e.txt root@ubuntu01:/home/test# ll -h total 14M drwxr-xr-x 2 root root 4.0K 3月 27 18:20 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 0 3月 27 18:18 b.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:15 c.txt -rw-r--r-- 1 root root 8.0M 3月 27 18:16 e.txt root@ubuntu01:/home/test# ls /home/test2/ root@ubuntu01:/home/test# find ./ -type f -size +5M ./e.txt root@ubuntu01:/home/test# find ./ -type f -size +5M -exec cp -a {} /home/test2/ \; ## 查找大小大于5M的文件,并复制至/home/test2目录下 root@ubuntu01:/home/test# ls /home/test2/ e.txt
10、查找最大文件、最小文件
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 196M drwxr-xr-x 2 root root 4.0K 3月 27 18:26 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 50M 3月 27 18:26 a.txt -rw-r--r-- 1 root root 10M 3月 27 18:26 b.txt -rw-r--r-- 1 root root 100M 3月 27 18:26 c.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:26 d.txt -rw-r--r-- 1 root root 30M 3月 27 18:26 e.txt root@ubuntu01:/home/test# find ./ -type f -exec ls -s {} \; ## 列出所有文件的大小 30720 ./e.txt 5120 ./d.txt 51200 ./a.txt 102400 ./c.txt 10240 ./b.txt root@ubuntu01:/home/test# find ./ -type f -exec ls -s {} \; | sort -n ## 依据数值大小排序 5120 ./d.txt 10240 ./b.txt 30720 ./e.txt 51200 ./a.txt 102400 ./c.txt root@ubuntu01:/home/test# find ./ -type f -exec ls -s {} \; | sort -n | tail -n 1 ## 取出最大值 102400 ./c.txt root@ubuntu01:/home/test# find ./ -type f -exec ls -s {} \; | sort -n | head -n 1 ## 取出最小值 5120 ./d.txt
补充:
root@ubuntu01:/home/test# ls a.txt b.txt c.txt d.txt e.txt root@ubuntu01:/home/test# ll -h total 196M drwxr-xr-x 2 root root 4.0K 3月 27 18:50 ./ drwxr-xr-x 6 root root 4.0K 3月 27 14:36 ../ -rw-r--r-- 1 root root 50M 3月 27 18:26 a.txt -rw-r--r-- 1 root root 10M 3月 27 18:26 b.txt -rw-r--r-- 1 root root 100M 3月 27 18:26 c.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:26 d.txt -rw-r--r-- 1 root root 30M 3月 27 18:26 e.txt root@ubuntu01:/home/test# ls -Shl ## 所有文件从大到小排序 total 195M -rw-r--r-- 1 root root 100M 3月 27 18:26 c.txt -rw-r--r-- 1 root root 50M 3月 27 18:26 a.txt -rw-r--r-- 1 root root 30M 3月 27 18:26 e.txt -rw-r--r-- 1 root root 10M 3月 27 18:26 b.txt -rw-r--r-- 1 root root 5.0M 3月 27 18:26 d.txt root@ubuntu01:/home/test# ls -Shl | sed '1d' | awk '{print $9, $5}' ## 输出文件名和文件大小 c.txt 100M a.txt 50M e.txt 30M b.txt 10M d.txt 5.0M root@ubuntu01:/home/test# ls -Shl | sed '1d' | awk '{print $9, $5}' | head -n 1 ## 最大文件 c.txt 100M root@ubuntu01:/home/test# ls -Shl | sed '1d' | awk '{print $9, $5}' | tail -n 1 ## 最小文件 d.txt 5.0M
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-03-27 c语言中输入数组元素的值