一丶概述
1.find的简介及优缺点
find命令主要用于文件搜索,它的功能非常强大,可以根据不同的标准搜索任何文件,可以在任何位置进行检索。
优点:
• 精确查找
• 实时查找
缺点
•查找速度略慢
• 可能只搜索此用户具备读取和执行权限的目录
2.find语法
find [OPTION]... [查找路径] [查找条件] [处理动作]
查找路径:指定具体目标路径;默认为当前目录
查找条件:指定的查找标准,可以文件名、大小、类型、
权限等标准进行;默认为找出指定路径下的所有文件
处理动作:对符合条件的文件做操作,默认输出至屏幕
二丶条件及处理
1.条件
1.1根据文件名和inode查找:
-name "文件名称":支持使用glob
[root@centos7 etc]# find /etc/ -name pa**wd
/etc/pam.d/passwd
/etc/passwd
-iname "文件名称":不区分字母大小写
[root@centos7 etc]# find /etc/ -iname pAs?wd
/etc/pam.d/passwd
/etc/passwd
-inum n 按inode号查找
[root@centos7 etc]# find /etc/ -inum 34663493
/etc/passwd
links n 链接数为n的文件
[root@centos7 etc]# find -links 2
1.2根据属主、属组查找:
-user USERNAME:查找属主为指定用户(UID)的文件
[root@centos7 app]# find /app/ -user root
/app/
/app/jiaoben
/app/jiaoben/ksjiaoben
-group GRPNAME: 查找属组为指定组(GID)的文件
[root@centos7 app]# find /app/ -group root
/app/
/app/jiaoben
/app/233.sh
/app/passwd
-uid UserID:查找属主为指定的UID号的文件
[root@centos7 app]# find /app/ -uid 1000
-gid GroupID:查找属组为指定的GID号的文件
[root@centos7 app]# find /app/ -gid 1000
-nouser:查找没有属主的文件
[root@centos7 app]# find /app/ -nouser
-nogroup:查找没有属组的文件
[root@centos7 app]# find /app/ -nogroup
1.3根据文件类型查找:
-type TYPE:
f: 普通文件
d: 目录文件
l: 符号链接文件
s: 套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件
find /dev/ -type c
1.4根据时间戳:
1.4.1以“天”为单位:
-atime [+|-]#,
#: [#,#+1)
+#: [#+1,∞]
-#: [0,#)
(注: [ 为包括此数字,)为不包括此数字)
-mtime
-ctime
[root@centos7 app]# find /app/ -mtime 7
(此为显示从7天到第8天内的所有文件目录【包含第七天,不包括第八天】)
1.4.2以“分钟”为单位:
(基本同上)
-amin -mmin -cmin
5.根据权限查找:
MODE: 精确权限匹配
/MODE:任何一类(u,g,o)对象的权限中只要能一位匹配
即可,或关系,+ 从centos7开始淘汰
-MODE:每一类对象都必须同时拥有指定权限,与关系
0 表示不关注
• find -perm 755 会匹配权限模式恰好是755的文件
• 只要当任意人有写权限时,find -perm +222就会匹配
• 只有当每个人都有写权限时,find -perm -222才会匹配
• 只有当其它人(other)有写权限时,find -perm -002才
会匹配
2.处理动作
-print:默认的处理动作,显示至屏幕
-ls:类似于对查找到的文件执行“ls -l”命令
[root@centos7 app]# find /app/ -name passwd -ls
delete:删除查找到的文件
[root@centos7 app]# find /app/ -name passwd -delete
fls file:查找到的所有文件的长格式信息保存至指定文件中
[root@centos7 app]# find /app/ -name passwd > passwdfile
ok -exec ok交互式,exec直接运行
find -name ".tmp" -ok rm -f {} \;
find -type f -name ".sh" -exec chmod a+x {} \;
find -name "*.conf" -exec cp -i {} {}.bak \;
转载于:https://blog.51cto.com/13570215/2065741