您的位置:首页 > 其它

find命令详解

2015-12-24 19:59 337 查看
写在前面,命令总览:
文件名:-name -iname glob 从属关系: -user -group -uid –gid -nouser -nogroup
按类型:-type [] ,f,d,l,b,c,p,s 组合测试:逻辑与或非-a -o ,-not,! 大小:-size 时间:-atime –mtime
-amin 权限:-perm 处理动作:-print -ls -delete -fls -ok -exec
find命令一、简介
find是一款文件查找工具,非常适合查找计算机上的文件。
二、与locate优缺点比较
locate:
优点:查找速度快,自动更新数据库
缺点:依赖于数据库,非实时查找,结果非精确,模糊查找
find:
优点:精确匹配,实时查找,通过遍历指定路径文件系统层级结构完成文件查找
缺点:查询速度较慢
find用法:
find[OPTIONS] [查找起始路径] [查找条件] [处理动作]
查找起始路径:指定具体搜索目标起始路径;默认为当前目录;
查找条件:指定的查找标准,可以根据文件名、大小、类型、从属关系、权限等等标准进行;
默认为找出指定路径下的所有文件;
处理动作:对符合查找条件的文件做出的操作,例如删除等操作;默认为输出至标准输出;

找条件:
测试:结果常为布尔型(真假)
根据文件名查找:
-name”pattern“
-iname“pattern”, ignorecase 忽略大小写
[root@yph7 tmp]# find /tmp -iname"gentoo"
/tmp/GENtooglob支持文件名通配
*,?, [], [^][root@yph7 tmp]# find /tmp -iname "gen*"/tmp/GENtoo/tmp/gentext根据文件从属关系查找:
-userUSERNAME:查找属主指定用户的所有文件;
[root@yph7 tmp]#find /tmp -user breeze –ls -------- -ls指查看详细属性
3552 8 -rw-rw-r-- 1 breeze breeze 19 12月 16 06:13/tmp/a.breeze
67202546 4 -rw------- 2 breeze root 2800 12月 18 06:17 /tmp/dir.root/passwd.root
67202546 4 -rw------- 2 breeze root 2800 12月 18 06:17 /tmp/dir.root/passwd.root.link
-groupGRPNAME:查找属组指定组的所有文件;
[root@yph7 tmp]#find /tmp -group apache -ls
33554819 0 drwxrwsr-t 2 root apache 51 12月 16 05:42 /tmp/text
33554841 0 -rw-rw-r-- 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
-uidUID:查找属主指定的UID的所有文件;
[root@yph7 tmp]#id -u hadoop
2051
[root@yph7 tmp]#find /tmp -uid 2051 -ls
33554841 0 -rw-rw-r-- 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
33554840 0 -rw-rw-r-- 1 hadoop apache 0 12月 16 05:34 /tmp/text/c.hadoop
-gid GID:查找属组指定的GID的所有文件;
[root@yph7 tmp]#id -g breeze
1004
[root@yph7 tmp]#find ./ -gid 1004 -ls
3552 8 -rw-rw-r-- 1 breeze breeze 19 12月 16 06:13./a.breeze
-nouser:查找没有属主的文件;
[root@yph7 tmp]#find ./ -nouser -ls
283 0 -rw-r--r-- 1 2003 hadoop 0 12月 20 20:06./a.mysql1
-nogroup:查找没有属组的文件;
[root@yph7 tmp]#find /home -nogroup -ls
255 0 drwx------ 3 breeze 4002 89 12月 13 05:52/home/fedora
根据文件的类型查找:
-type TYPE
f: 普通文件
[root@yph7 tmp]#find ./ -type f -ls
282 0 -rw-r--r-- 1 root root 0 12月 20 19:48./gentext
283 0 -rw-r--r-- 1 2003 hadoop 0 12月 20 20:06./a.mysql1
d: 目录文件
[root@yph7 tmp]#find /tmp -type d -ls
133 4 drwxrwxrwt 11 root root 4096 12月 20 20:06 /tmp
67156266 0 drwxrwxrwt 2 root root 6 12月 8 21:20 /tmp/.X11-unix
l:符号链接文件
[root@yph7 tmp]#find /tmp -type l -ls
67202555 0 lrwxrwxrwx 1 root root 11 12月 18 06:21 /tmp/ passwd.symbolink -> passwd.root
b:块设备文件
[root@yph7 tmp]#find /dev -type b -ls
10821 0 brw-rw---- 1 root disk 8, 7 12月 20 18:02 /dev/sda7
10820 0 brw-rw---- 1 root disk 8, 6 12月 20 18:02 /dev/sda6
c:字符设备文件
p:管道文件
s:套接字文件

测试
-a,and逻辑与关系,为系统默认
[root@yph7 tmp]#find -name "pass*" -a -user flimmer –ls --------必须同时满足两个条件
274 4 -rw-r--r-- 1 flimmer flimmer 2800 12月 17 22:18 ./passwd.flimmer
-o,or逻辑或关系
[root@yph7 tmp]#find /tmp \( -user flimmer -o -name "pass*" \) -ls
270 4 -rw-r--r-- 1 root root 2800 12月 16 18:43/tmp/passwd
273 0 -rw-r--r-- 1 flimmer flimmer 0 12月 17 22:16/tmp/a.flimmer
-not或者!:逻辑非关系
[root@yph7 tmp]#find /tmp -not -user root -ls
33554841 0 -rw-rw-r-- 1 hadoop apache 0 12月 16 05:09 /tmp/text/b.hadoop
33554831 4 -rw-rw-r-- 1 gentoo apache 95 12月 17 06:45 /tmp/text/c.gentoo
练习:
1、找出/tmp目录下文件名中不包含fstab字符串的文件;
[root@yph7 tmp]#find /tmp -not -name "*passwd*"
2、找出/tmp目录下属主为非root,而且文件名不包含fstab字符串的文件;
[root@yph7 tmp]# find/tmp -not -user root -a -not -name "*fstab*" -ls
[root@yph7 tmp]#find -not \( -user root -o -name "*fstab*" \) –ls
[root@yph7 tmp]#find ! \( -user root -o -name "*passwd*" \) –ls

根据文件大小查找:
-size[+|-]#UNIT 常用单位:k(小写), M, G
-5k:[0,5) --------小于5k ------个人理解
5k:(4,5] --------5K的文件,大于4K,小于等于5K
+5k:(5,+oo) -----------大于5k的文件
[root@yph7 tmp]# find /tmp -size -5k -----------小于5k的文件/tmp/tmp/.X11-unix/tmp/.ICE-unix
以天为单位查找:

-3 :过去的3天内,不包括等于第3天
3:过去的第二天到过去的第三天之间
+3 : 过去的第四天及第四天之前的时间,至少已经三天没访问了

-atime[+|-]#:最近一次访问时间
[root@yph7 text]# find ./ -atime -3 ---------查看三天内访问过的文件./ ------------没有三天内访问过的文件 [root@yph7text]# ll总用量 4-rw-rw-r--. 1 gentoo apache 95 12月 17 06:45 c.gentoo-rw-rw-r--. 1 hadoop apache 0 12月 16 05:34 c.hadoop [root@yph7text]# cat c.gentoo &> /dev/null -------现在就访问一次 [root@yph7text]# find ./ -atime -3././c.gentoo [root@yph7 text]# cat ./b.hadoop --------发现就找到了 [root@yph7 text]# stat ./c.gentoo ------------用stat查看一下c.gentoo的时间戳 文件:"./c.gentoo" 大小:95 块:8 IO 块:4096 普通文件设备:802h/2050d Inode:33554831 硬链接:1权限:(0664/-rw-rw-r--) Uid:( 4006/ gentoo) Gid:( 2011/ apache)环境:unconfined_u:object_r:user_tmp_t:s0最近访问:2015-12-20 22:14:40.159540734 +0800最近更改:2015-12-17 06:45:08.922153037 +0800最近改动:2015-12-17 06:45:08.922153037 +0800-mtime最近一次修改文件内容时间
-ctime 最近一次修改元数据的时间。权限属性等,
[root@yph7 text]# find ./ -ctime -1 -----------最近一天内修改过元素据的文件,发现没有./[root@yph7 text]# chmod o+w c.gentoo -------修改下元素据,更改下权限[root@yph7 text]# find ./ -ctime -1././c.gentoo ------------就能找到了以分钟为单位查找 用法与根据天查找相同
-amin:最近一次访问时间,以分钟为单位
[root@yph7 tmp]# find ./ -amin -3 -------查找3分钟内访问过的文件,发现没有[root@yph7 tmp]# cat ./c.root > /dev/null --------现在访问一次[root@yph7 tmp]# find ./ -amin -3./c.root ------------就能查找到了-mmin:最近一次修改内容时间,分钟为单位
[root@yph7 tmp]# find ./ -mmin +3 ---查找三分钟之前修改过内容的文件,发现所有文件都列了出来./ -----因为三分钟内没有修改过文件内容,所有文件都符合条件./.X11-unix./.ICE-unix./.XIM-unix………………………………………-cmin:最近一次修改元数据时间,分钟为单位,权限属性等

根据权限查找
-perm [/|-]mode
mode:精确权限匹配
[root@yph7 text]#find ./ -perm 664 –ls --------查找权限为664的文件
33554840 0 -rw-rw-r-- 1 hadoop apache 0 12月 16 05:34 ./c.hadoop
/mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足;只要有任何一个交集
9位权限之间存在“或”关系只需满足其中任何一个;比如一个权限为rw-r----的文件,为“-”的位就忽略不考虑,只考虑出现”r或w或x”这三个字母的位,即只认为有r,w,r,权限,如果模式为/034,即----wx---,文件权限中有一个w,模式中也有一个w这就匹配到了。如果为/100,即x--------,只有一个x,但文件权限例一个x都没有,就不能匹配到。
[root@yph7 text]# chmod 640 c.gentoo[root@yph7 text]# find ./ -perm /034 -ls33554819 0drwxrwsr-t 2 root apache 36 12月 20 22:13 ./33554840 0-rw-rw-r-- 1 hadoop apache 0 12月 16 05:34./c.hadoop[root@yph7 text]# find ./ -perm /100./[root@yph7 text]# -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足;完全包括模式
9位权限之间存在“与”关系;即u,g,o三类用户的权限都必须多与或等于模式给出的对应的权限。比如A文件权限为rw-r-----,B文件权限为rw-rw-r--,模式rw-rw------满足B不能满足A,r---r-----能同时满足AB。要想满足A就必须少与或等于rw-r-----,要满足B就必须少于或等于rw-rw-r--.
[root@yph7 text]#ll
总用量 4
-rw-r-----. 1gentoo apache 95 12月 17 06:45 c.gentoo
-rw-rw-r--. 1hadoop apache 0 12月 16 05:34 c.hadoop
[root@yph7 text]#find ./ -perm -444
./
./c.hadoop
[root@yph7 text]# find ./ -perm -400
./
./c.gentoo
./c.hadoop
[root@yph7 text]#find ./ -perm -440
./
./c.gentoo
./c.hadoop
[root@yph7 text]#find ./ -perm -664
./
./c.hadoop
[root@yph7 text]#find ./ -perm -663
[root@yph7 text]#find ./ -perm -640
./
./c.gentoo
./c.hadoop


-print:输出至标准输出;默认的动作;
-ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息;
-delete:删除查找到的文件;
[root@yph7 text]# ll总用量 4-rw-r-----. 1 gentoo apache 95 12月 17 06:45 c.gentoo-rw-rw-r--. 1 hadoop apache 0 12月 16 05:34 c.hadoop [root@yph7 text]# find ./ -perm -444 -deletefind: 无法删除 ‘./’: 无效的参数[root@yph7 text]# ll总用量 4-rw-r-----. 1 gentoo apache 95 12月 17 06:45 c.gentoo ----c.hadoop已经被删掉了-fls/PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
[root@yph7 tmp]# cat c.root[root@yph7 tmp]# find -name "pass*" -fls./c.root[root@yph7 tmp]# cat c.root 270 4 -rw-r--r-- 1 root root 2800 12月 16 18:43 ./passwd 274 4 -rw-r--r-- 1 flimmer flimmer 2800 12月 17 22:18 ./passwd.flimmer67202546 4-rw------- 2 breeze root 2800 12月 18 06:17 ./dir.root/passwd.root67202546 4-rw------- 2 breeze root 2800 12月 18 06:17 ./dir.root/passwd.root.link-okCOMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令;每次操作都由用户进行确认;
-execCOMMAND {} \; :对查找到的每个文件执行由COMMAND表示的命令;
注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;
但是有些命令不能接受过长的参数,此时命令执行会失败;另一种方式可规避此问题:
find | xargsCOMMAND

[root@yph7 text]#find ./ -perm -440 -ls -ok mv {} {}.bak\; --------会询问是否要进行
33554819 0 drwxrwsr-t 2 root apache 21 12月 20 23:56 ./
< mv ... ./> ? y
mv: 无法将"./" 移动至"./.bak": 设备或资源忙
33554831 4 -rw-r----- 1 gentoo apache 95 12月 17 06:45 ./c.gentoo
< mv ..../c.gentoo > ? y
[root@yph7 text]#ll
总用量 4
-rw-r-----. 1gentoo apache 95 12月 17 06:45 c.gentoo.bak

[root@yph7 text]#find ./ -perm -440 -ls
33554819 0 drwxrwsr-t 2 root apache 21 12月 20 23:22 ./
33554831 4 -rw-r----- 1 gentoo apache 95 12月 17 06:45 ./c.gentoo
[root@yph7 text]#find ./ -perm -440 -ls -exec mv {} {}.bak \; -------不会询问你,直接进行
[root@yph7 text]#ll
总用量 4
-rw-r-----. 1gentoo apache 95 12月 17 06:45 c.gentoo.bak

[root@yph7 tmp]#find /etc -name "issue*" -exec cat {} &> /tmp/c.root \;
[root@yph7 tmp]#cat /tmp/c.root
\S
Kernel \r on an\m
Mage EducationLearning Services http://www.magedu.com \S
Kernel \r on an\m

练习:
1、查找/usr目录下不属于root, bin或hadoop的所有文件或目录;用两种方法;
[root@yph7 tmp]#find /usr -not -user root -a -not -user bin -a -not -user hadoop
[root@yph7 tmp]#find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
2、查找/etc目录下最近一周内其内容修改过,且属主不是root用户也不是hadoop用户的文件或目录;
[root@yph7 tmp]#find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls
[root@yph7 tmp]#find /etc -mtime -7 -a -not -user root -a -not -user hadoop -ls

3、查找当前系统上没有属或属组,且最近一周内曾被访问过的文件或目录;
[root@yph7 init.d]# find / \( -nouser -o -nogroup \) -a -atime -7 –ls
---必须加括号,否则-o与-a顺序不清楚 283 0 -rw-r--r-- 1 2003 hadoop 0 12月 20 20:06/tmp/a.mysql1
255 0 drwx------ 3 breeze 4002 89 12月 13 05:52/home/fedora
4、查找/etc目录下大于1M且类型为普通文件的所有文件;
[root@yph7 text]#find /etc -size +1M -exec ls -lh {} \;
-r--r--r--. 1root root 6.1M 12月 8 19:47 /etc/udev/hwdb.bin
-rw-r--r--. 1root root 3.7M 3月 6 2015/etc/selinux/targeted/policy/policy.29
5、查找/etc目录下所有用户都没有写权限的文件;
[root@yph7 text]#find /etc -not -perm /222 -type f -ls
6、查找/etc目录至少有一类用户没有执行权限的文件;
[root@yph7 text]# find /etc -not -perm -111 -type f –ls
67198001 4 -rw-r--r-- 1 root root 19 12月 8 20:43 /etc/locale.conf
67198008 12 -rw-r--r-- 1 root root 12288 12月 8 20:45 /etc/aliases.db
67202533 4 -rw-r--r-- 1 root root 17 12月 9 21:09 /etc/hostname
7、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的所有文件;
[root@yph7 tmp]#find /etc -perm -113 -type f -ls
[root@yph7 text]#find /etc/init.d -perm -111 -a -perm -002 -type f -ls

本文出自 “风吹草低见牛羊” 博客,请务必保留此出处http://flivfox.blog.51cto.com/10720563/1728010
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: