Linux-文件查找

grep, egrep, fgrep: 文本查找


文件查找:
locate:
非实时,模糊匹配,查找是根据全系统文件数据库进行的;
# updatedb, 手动生成文件数据库
速度快


find:
实时
精确
支持众多查找标准
遍历指定目录中的所有文件完成查找,速度慢
find 查找路径 查找标准 查找到以后的处理运作
查找路径:默认为当前目录
查找标准:默认为指定路径下的所有文件
处理运作:默认为显示


匹配标准:
-name 'FILENAME':对文件名作精确匹配
文件名通配:
*:任意长度的任意字符
?
[]
-iname 'FILENAME': 文件名匹配时不区分大小写
-regex PATTERN:基于正则表达式进行文件名匹配

-user USERNAME: 根据属主查找
-group GROUPNAME: 根据属组查找

-uid UID: 根据UID查找(如果用户或者组删除,文件就会使用UID,GID)
-gid GID: 根据GID查找

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


-type 
f: 普通文件
d
c
b
l
p
s

-size [+|-] (注意:+10k -> >10k,默认字节)
#k
#M
#G

eg:find /etc -size +100k -ls
组合条件:
-a
-o
-not 
eg:find /etc -size +100k -a -user root -a -type f -ls
find /etc -not -type f -ls




stat * 查看当前目录下文件的时间戳信息
根据时间戳查找:

-mtime 修改 
-ctime 改变
-atime 访问
[+|-]#(单位天)
-mmin
-cmin
-amin
[+|-]#(单位分钟)

eg:find /etc/test -amin -5 5分钟以内访问过的文件,stat *来检验
根据权限来查找:
-perm MODE:精确匹配
/MODE: 任意一位匹配即满足条件,OR
-MODE: 文件权限能完全包含此MODE时才符合条件,AND

-644
644: rw-r--r--
755: rwxr-xr-x 包含644,可以
750: rwxr-x--- 不包含,不可以
find ./ -perl -001
查找到以后的处理运作:
-print: 显示
-ls:类似ls -l的形式显示每一个文件的详细
-ok COMMAND {} \; 每一次操作都需要用户确认
-exec COMMAND {} \;

eg:find ./ -type f -ok chmod +x {} \;

这里的{}表示对匹配文件的引用
eg:find ./ -perm -001 -exec mv {} {}.update \;


练习题:
1、查找/var目录下属主为root并且属组为mail的所有文件;
find /var -user root -group mail
2、查找/usr目录下不属于root,bin,或student的文件;
find /usr -not -user root -a -not -user bin -a -not -user student
find /usr -not \( -user root -o -user bin -o -user student \)
3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件;
find /etc -mtime -7 -not \ ( -user root -o -user student \)
find /etc -mtime -7 -not -user root -a -not -user student
4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root;
find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \; 
5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中;
find /etc -size +1M -exec echo {} >> /tmp/etc.largefiles \;
find /etc -size +1M >> /tmp/etc.largefiles
find /etc -size +1M | xargs echo  >> /tmp/etc.largefiles
6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息;
find /etc -not -perm /222 -ls


find指令查找后,要执行额外命令的时候,除了-ok,-exec外,还有xargs,

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值