find 命令

Just Do It
2023-11-01 / 0 评论 / 8 阅读 / 正在检测是否收录...

文件查找概述

Linux系统中的find命令在查找文件时非常有用而且方便。
它可以根据不同的条件来进行查找文件:例如权限、拥有者、修改日期/时间、文件大小等等。 同时find命令是Linux下必须掌握的。

find 命令的基本语法如下

命令 路径 选项 表达式 动作
find [path...] [options] [expression] [action]
查找 地区 犯罪嫌疑人 性别男25-30岁 枪决行动

find名称查找


touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}


[root@xuliangwei ~]# find /etc -name "ifcfg-eth1"


[root@xuliangwei ~]# find /etc -iname "ifcfg-eth1"

[root@xuliangwei ~]# find /etc/ -name "ifcfg-eth*"
[root@xuliangwei ~]# find /etc -iname "ifcfg-eth*"

find大小查找


[root@xuliangwei ~]# find /etc -size +5M


[root@xuliangwei ~]# find /etc -size 5M


[root@xuliangwei ~]# find /etc -size -5M 

find时间查找


[root@xuliangwei ~]# for i in {01..28};do date -s  201802$i && touch file-$i;done


[root@xuliangwei ~]# find ./ -iname "file-*" -mtime +7


[root@xuliangwei ~]# find ./ -iname "file-*" -mtime -7


[root@xuliangwei ~]# find ./ -iname "file-*" -mtime 7


find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +90 -delete

find用户查找

//查找属主是jack
[root@xuliangwei ~]
//查找属组是admin
[root@xuliangwei ~]
//查找属主是jack, 属组是admin
[root@xuliangwei ~]
//查找属主是jack, 并且属组是admin
[root@xuliangwei ~]
//查找属主是jack, 或者属组是admin
[root@xuliangwei ~]
//查找没有属主
[root@xuliangwei ~]
//查找没有属组
[root@xuliangwei ~]
//查找没有属主或属组
[root@xuliangwei ~]

find类型查找


[root@xuliangwei ~]# find /dev -type f

[root@xuliangwei ~]# find /dev -type d

[root@xuliangwei ~]# find /dev -type l

[root@xuliangwei ~]# find /dev -type b

[root@xuliangwei ~]# find /dev -type c

[root@xuliangwei ~]# find /dev -type s

[root@xuliangwei ~]# find /dev -type p

find权限查找


[root@xuliangwei ~]# find . -perm 644 -ls


[root@xuliangwei ~]# find /home -perm -324 

[root@xuliangwei ~]# find . -perm -222 -ls


[root@xuliangwei ~]# find /home -perm /442 


[root@xuliangwei ~]# find  /usr/sbin -perm -4000 -ls

[root@xuliangwei ~]# find  /usr/sbin -perm -2000 -ls

[root@xuliangwei ~]# find  /usr/sbin -perm -1000 -ls

find处理动作

当查找到一个文件后, 需要对文件进行如何处理, 默认动作 -print

-print  
-ls     
-delete 
-exec   
-ok     



[root@xuliangwei ~]# find /etc -name "ifcfg*"
[root@xuliangwei ~]# find /etc -name "ifcfg*" -print
[root@xuliangwei ~]# find /etc -name "ifcfg*" -ls


[root@xuliangwei ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;


[root@xuliangwei ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;


[root@xuliangwei ~]# find /etc -name "ifcfg*" -exec rm -f {} \;
[root@xuliangwei ~]# find /etc -name "ifcfg*" -delete

find结合xargs

//xargs将查找到结果一个一个的处理
[root@xuliangwei ~]
[root@xuliangwei ~]
[root@xuliangwei ~]

转 xuliangwei.com

0

评论

博主关闭了当前页面的评论