Linux之路:常见文件查找方法

linux文件查找主要是locate、find.

locate

locate主要查找速度快,实时,但需要依赖系统文件数据库,而这个文件数据库需要手动生成:updatedb.

find用法

    实时,精确,有众多查找标准,它遍历指定目录的所有文件完成查找,速度慢。

    语法:

            find  查找路径  查找标准  查到后处理command

查找路径:默认为当前目录

查找标准:默认为指定路径下的所有文件

处理动作:默认为显示

匹配标准:

-name “filename”:对文件名进行精确匹配

文件名通配:

*:任意长度任意字符

?:

[]

-iname “filename” :文件名不区分大小写。‘

-regex  pattern  :基于正则表达式进行文件名匹配

-user User:属主属于user用户的文件

-group group 根据属组查找

-uid UID

-gid GID

-nouser :查找没有属主的文件

-nogroup:查找没有属组的文件

 

-type 

f:普通文件

d:目录

c:字符设备

b:块设备,二进制文件

l:链接文件

p:管道文件

s:套接字文件

-size

[+|-]#k

#M

#G

组合条件:【默认就是与条件】

-a与条件

-o或条件

-not 非条件


时间

-mtime修改时间

-ctime  改变时间

-atime  访问时间

[+|-]#

-perm 

 mode  精确查找

-mode  文件权限能完全包含此mode时才符合条件

/mode  任意一位能匹配就行

-ok command {} \;  每一次都需要用户确认

-exec command {}  \; 


常见案例练习

[root@desktop5 test]# find /etc/ -size +1M | xargs >> /tmp/etc.largefiles

1.查找/var目录下属主为root并且属组为mail所有文件

[root@desktop5 test]# find /var/ -user root -group mail > /tmp/var.rootmail.file


2.查看/usr/目录下不属于root,bin,或student的文件

[root@desktop5 test]# find /usr/ -not \( -user root -o -user bin -o -user student \) -exec ls -al {} \;

-rwsr-xr-x. 1 abrt abrt 9856 Jan 24  2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache


3.查找/etc/目录下最近一周内内容修改过且不属于root及student用户的文件

[root@desktop5 ~]# find /etc/ -mtime -7 -a -not \( -user root -o -user student \)

[root@desktop5 ~]# find /etc/ -mtime -7 -not -user root -a -not -user student


4.查找当前系统上没有属主或属组且最近一天曾被访问的文件,并将其属主属组修改为root

[root@desktop5 ~]# find / -atime -1 -nouser -nogroup | xargs chown  root:root {} \;


5.查找/etc/目录下大于1M的文件,并将其写入/tmp/etc.arggefile

[root@desktop5 ~]# find /etc/ -size +1M  >> largefile


6.查找/etc/目录下所有用户都没有写权限的文件,显示其详细信息

[root@desktop5 ~]# find /etc/ -not -perm /222


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章