2023.6.2linux系统文件查找
03.Linux系统⽂件查找
⽂件查找概述
find名称查找
find⼤⼩查找
find时间查找
find⽤户查找
find类型查找
find权限查找
find处理动作
Author vx: WingspanGo
⽂件查找概述
Linux 系统中的 find 命令在查找⽂件时⾮常有⽤⽽且⽅便。
它可以根据不同的条件来进⾏查找⽂件:例如⽂件名、修改⽇期/时间、权限、拥有者、⽂件⼤⼩等等。 同时 find
命令是 Linux 下必须掌握的。
find 命令的基本语法如下
find名称查找
//创建⽂件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
//查找/etc⽬录下包含ifcfg-eth0名称的⽂件
[root@wing ~]# find /etc -name "ifcfg-eth1"
//-i 忽略⼤⼩写
[root@wing ~]# find /etc -iname "ifcfg-eth1"
//查找/etc⽬录下包含ifcfg-eth名称所有⽂件
[root@wing ~]# find /etc/ -name "ifcfg-eth*"
[root@wing ~]# find /etc -iname "ifcfg-eth*"
find⼤⼩查找
//查找⼤于5M
[root@wing ~]# find /etc -size +5M
//超找等于5M
[root@wing ~]# find /etc -size 5M
//查找⼩于5M
[root@wing ~]# find /etc -size -5M
find时间查找
//创建测试⽂件
[root@wing ~]# for i in {01..28};do date -s 202302$i && touch file-$i;done
//查找7天以前的⽂件(不会打印当天的⽂件)
[root@wing ~]# find ./ -iname "file-*" -mtime +7
//查找最近7天的⽂件,不建议使⽤(会打印当天的⽂件)
[root@wing ~]# find ./ -iname "file-*" -mtime -7
//查找第7天⽂件(不会打印当天的⽂件)
[root@wing ~]# find ./ -iname "file-*" -mtime 7
//本地⽂件保留最近7天的备份⽂件, 备份服务器保留3个⽉的备份⽂件
//本地⽂件保留最近7天的备份⽂件, 备份服务器保留3个⽉的备份⽂件
find ./ -iname "file-*" -mtime +7 -delete
find /backup/ -iname "*file-*" -mtime +90 -delete
find⽤户查找
//查找属主是jack
[root@wing ~]# find /home -user jack
//查找属组是admin
[root@wing ~]# find /home -group admin
//查找属主是jack, 属组是admin
[root@wing ~]# find /home -user jack -group admin
//查找属主是jack, 并且属组是admin
[root@wing ~]# find /home -user jack -a -group admin
//查找属主是jack, 或者属组是admin
[root@wing ~]# find /home -user jack -o -group admin
//查找没有属主
[root@wing ~]# find /home -nouser
//查找没有属组
[root@wing ~]# find /home -nogroup
//查找没有属主或属组
[root@wing ~]# find /home -nouser -o -nogroup
find类型查找
//f ⽂件
[root@wing ~]# find /dev -type f
//d ⽬录
[root@wing ~]# find /dev -type d
//l 链接
[root@wing ~]# find /dev -type l
//b 块设备
[root@wing ~]# find /dev -type b
//c 字符设备
[root@wing ~]# find /dev -type c
//s 套接字
[root@wing ~]# find /dev -type s
//p 管道⽂件
[root@wing ~]# find /dev -type p
find权限查找
//精切匹配644权限
[root@wing ~]# find . -perm 644 -ls
//查找全局可写(没位权限必须⾼于2 -w-)
[root@wing ~]# find . -perm -222 -ls
//拥有者⾄少有r权限, 或者拥有组⾄少有r权限, 或者匿名⾄少有w权限
[root@wing ~]# find /home -perm 442 #r 2w
//包含set uid
[root@wing ~]# find /usr/sbin -perm -4000 -ls
//包含set gid
[root@wing ~]# find /usr/sbin -perm -2000 -ls
//包含sticky
[root@wing ~]# find /usr/sbin -perm -1000 -ls
find处理动作
当查找到⼀个⽂件后, 需要对⽂件进⾏如何处理, 默认动作 -print
-print //打印
-ls //以⻓格式打印显示
-delete //删除查找到的⽂件(仅能删除空⽬录)
-exec //后⾯跟⾃定义的 shell 命令(标准写法 -exec \;)
-ok //后⾯跟⾃定义的 shell 命令(会提示是否操作)
//打印查询到的⽂件
[root@wing ~]# find /etc -name "ifcfg*"
[root@wing ~]# find /etc -name "ifcfg*" -print
[root@wing ~]# find /etc -name "ifcfg*" -ls
//拷⻉⽂件
[root@wing ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
//-ok会不断提示
[root@wing ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
//删除⽂件
[root@wing ~]# find /tmp -name "ifcfg*" -exec rm -f {} \;
[root@wing ~]# find /tmp -name "ifcfg*" -delete
find结合xargs
//xargs将查找到结果⼀个⼀个的处理
[root@wing ~]# touch file.txt
[root@wing ~]# find . -name "file.txt" |xargs rm -f
[root@wing ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)