find 搜索文件系统 实时搜索
find [目录] [条件] [动作]
【目录】
不输入代表当前目录
find
find /boot
[条件]
用户和组 -user -group -nouser -nogroup
查找/home 下拥有者为 mk 的文件:
[root@localhost home]# find /home/ -user yang
[root@localhost home]# find /home/ -nouser yang
[root@localhost home]# find /home/ -group yang
[root@localhost home]#find /home/ -nogroupyang
【类型】 -type(f 文件, d 目录, l 连接, p 管道, c 字符文件, b 块文件, s socket文件)
[root@localhost home]# find /home/ -type d
[root@localhost home]#ll /dev/sda1
brw-rw---- 1 root disk 8, 1 Dec 1 21:04 /dev/sda1
[root@localhost home]#find /dev/ -type b #查找块文件
【名字】 -name
[root@localhost home]#useradd user1
[root@localhost home]# find /home/ -name *user*
/home/user1
【大小】-size +NM 大于N兆 -NG 小于NGB
[root@localhost home]#find /boot/ -size -2G
【时间】 -mtime -atime -ctime
[root@localhost home]#find /tmp/ -mtime 1
-mtime +2 查看不含今天的2天前被修改的所有文件, 如:今天是 12月1日, 则找出3日及3日之前,被修改的内容
扩展:Linux中 ctime, mtime, atime的区别
ctime : "改变时间(change time)"
mtime: "修改时间(modify time)"
atime : "访问时间(access time)"
改变和修改之间的区别在于改文件的属性还是改文件内容
ls(1) 命令可用来列出文件的 atime, ctime 和 mtime
ls -lc filename 列出文件的ctime
ls -lu filename 列出文件的atime
ls -l filename 列出文件的mtime
【权限】 -perm
[root@localhost ~]# find /boot/ -perm 0755 #找到等于0755权限的文件或目录
[root@localhost ~]# find /tmp -perm 1777 #sticky 1
[root@localhost ~]# find /tmp -perm 2777 #suid 2
[root@localhost ~]# find /tmp -perm 4777 #suid 4
[root@localhost ~]# find /tmp -perm -777 #减号代表 至少有777权限的文件
【目录深度】-maxdepth N #N 层次深度
[root@localhost ~]# find /boot -maxdepth 1
【多条件】
-a -o ! 或着 -and -or -not 与 或 非
在/root下找到其他人可以写的文件
[root@localhost ~]# find -type f -and -perm -002
./.ssh/id_rsa
./.ssh/id_rsa.pub
[root@localhost ~]# find -type f -and -perm /o+w
./.ssh/id_rsa
./.ssh/id_rsa.pub
其它人可以执行的非普通文件的文件
[root@localhost ~]#find ! -type f -and -perm -001
./.cache
./.cache/abrt
./.config
【动作】
-ls
-ok
-exec
-printf
[root@localhost ~]#find test2/ -type f -exec rm {} \;
参数解释:
-exec 执行命令
rm 要执行的命令
{} 表示find -type f 查找出来的文件内容
\; {}和\;之间要有空格 固定语法 就是以这个结尾
[root@localhost ~]#find test2/ -type f -exec dos2unix {} \;