您的位置:首页 > 运维架构 > Linux

Linux find查找

2015-09-07 22:20 507 查看
Linuxfind查找
1. 命令locate
linux每晚会汇总当天文件生成文件数据库,local根据全系统数据库进行的查找,非实时,模糊匹配,速度快。
updatedb:手动生成文件数据库,耗时。

2. 命令find
实时性、精确、支持众多查找条件、遍历指定目录中的所有文件完成查找,速度慢。

语法:find 查找路径 查找标准 查找到后处理动作
默认:查找路径:默认为当前目录。
查找标准:默认为指定路径下的所有文件。
查找到后处理动作:默认位显示。
匹配条件:
1. -name:指定文件名称,精确匹配; *:任意长度的任意字符。
[root@localhost~]# find /etc -name 'passwd'
/etc/passwd
/etc/pam.d/passwd
2. –iname:指定文件名不区域大小写。
[root@localhost ~]# find /root -inameluosttest
/root/luosttest
/root/Luosttest
3. –regexPATTERN:基于正则表达式进行文件名的查找。
4. –userusername:基于用户所属的查找。
[root@localhost~]# find /home -user pg
/home/pg
/home/pg/.viminfo
/home/pg/.mozilla
5. –groupgroupname:根据所组查找。
[root@localhost~]# find /home -group user1
/home/user1
/home/user1/.mozilla
/home/user1/.mozilla/plugins
6. –uid:根据用户id查找。
[root@localhost~]# find /home -uid 500
7. –gid:根据组用户id查找。
[root@localhost~]# find /home -gid 5005
/home/user1
8. –nouser:查找没有属主的文件,经常使用此命令,清除没有属主的文件。
[root@localhost~]# find /home –nouser
9. -nogroup:查找没有属组的文件。
[root@localhost~]# find /home –nogroup
10. –typex:基于文件类型的查找。
f:普通文件
d:目录
c:
l:链接文件
[root@localhost ~]# find /root -type d #查找是目录的文件。

11. –size x :基于文件大小的查找。
x k:单位位k
x M:单位为M
x G: 单位为G
[root@localhost ~]# find /root -size 10k #查找文件大小为10k的文件。
[root@localhost ~]# find /root -size 1M #查找文件大小为1M的文件
[root@localhost ~]# find /root -size -10k #查找文件大小小于10k的文件。+
[root@localhost ~]# find /root -size +10k #查找文件大小大于10k的文件。

12. 组合条件(与、或、非)
-a:与关系
-o:或关系
-not:非关系
Eg:
[root@localhost etc]# find /tmp -nouser -a-type d #查找tmp文件下没有属主并且文件类型是目录的文件。
[root@localhost etc]# find /tmp -nouser -o -typed #查找tmp文件下没有属主或者文件类型是目录的文件。
[root@localhost ~]# find /tmp -not -type d #查找tmp下不是目录的文件。
[root@localhost home]# find /home -not-user user1 -a -not -user user2 #既不是user1也不是user2的文件。
[root@localhost home]# find /home -not \(-user user1 -o -user user2 \) #摩根定律。取反。

13. 时间查找
-mtime:按文件更改时间来查找文件。
-ctime:按文件创建时间来查找文件。
-atime:按文件访问时间来查找文件。
Eg:
[root@qzt ~]# find /root -mtime -10 #查找10天内修改过的文件。
[root@qzt ~]#find /root -mtime +10 #查找10天以前修改过的文件。
[root@qzt ~]# find /root -ctime -10 #查找10天内创建的文件。
[root@qzt ~]# find /root -ctime +10 #查找10天以前创建的文件。
[root@qzt ~]# find /root -atime -10 #查找10天内访问过的文件。
[root@qzt ~]# find /root -atime +10 #查找10天前访问过的文件。

14. -perm:按照执行权限来查找。
[root@qzt ~]# find /root -perm 755 #查找执行权限为755的文件。

15. -newer:找文件修改时间在某一文件修改后的文件。
[root@localhost ~]# find -newer /root/test1 #test1修改之后修改过的文件。

16. -depth:先查找本目录,然后进入子目录查找。
[root@localhost ~]# find /etc /home /root-depth -name test1 #先查找目录/etc /home /root,再进入自目录查找。
/root/test1

17. -fstype: 查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到。
[root@localhost ~]# find -fstype ext4

18. -mount: 查文件时不跨越文件系统mount点。

19. -follow:如果遇到符号链接文件,就跟踪链接所指的文件
20. -cpio:查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
21. -prune:忽略某个目录
----------------------------------------------------------------------------------------

本文出自 “光与影” 博客,请务必保留此出处http://luost.blog.51cto.com/922727/1692524
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: