find查找
递归式查找
只能查找硬盘的数据
格式: find 查找的路径 条件
-type f(文件) d(目录) l(快捷方式)
[root@A /]# find /boot -type d
[root@A /]# find /opt -type d
[root@A /]# find /etc -type l
[root@A /]# find /boot -type f
[root@A /]# find /usr -type d
[root@A /]# find /var -type d
[root@A /]# find /sbin -type l
[root@A /]# find /bin -type f
-name支持通配符* 号
-iname 忽略大小写
]# find /etc -name "passwd"
]# find /etc -name "*tab"
]# find /etc -name "*.conf"
]# find /root -name ".*" #查找隐藏数据
]# find /etc -name "*tab" | wc -l
]# find /etc -name "*.conf" | wc -l
]# find /etc -name "*.conf" | cat -n
两个条件联合使用
]# mkdir /mnt/cbd01
]# mkdir /mnt/cbd02
]# touch /mnt/cbd03.txt
]# find /mnt/ -name "cbd*"
]# find /mnt/ -name "cbd*" -type d
]# find /mnt/ -name "cbd*" -type f
]# find /mnt/ -name "cbd*" -o -type f #两个满足其中一个
-size k M G
]# find /boot -size +300k
]# find /boot -size +10M
]# find /boot -size +20M
]# find /boot -size +10M -size -50M
-user 数据的所有者
[root@A /]# useradd natasha #创建用户
[root@A /]# find /home -user natasha
[root@A /]# find / -user natasha
/proc:内存的数据,不占用硬盘空间
[root@A /]# useradd harry #创建用户
[root@A /]# find /home -user harry
[root@A /]# find / -user harry
-mtime数据修改时间
+90 #90天之前的数据,三个月之前
-10 #最近10天之内
三个月之前的数据:
[root@A /]# find /var -mtime +90
最近10天之内的数据:
[root@A /]# find /root -mtime -10
补充:
-newermt #在此时间之后
!-newermt #不在此时间之后(在此时间之前)
在2024-4-30 10:00:00时间之后
]# find /var/ -newermt '2024-4-30 10:00:00'
在今天10:00:00时间之后,在今天14:00时间之前
]# find /var/ -newermt '10:00:00' ! -newermt '14:00:00'
find高级使用:处理find查找的数据
处理find找到的数据,每查找的一个就传递一次
find [范围] [条件] -exec 处理命令{} \;
-exec额外操作的开始
{} 永远表示前面find查找的结果
\; 额外操作的结束
]# find /boot -size +10M
]# find /boot -size +10M -exec cp {} /mnt \;
]# ls /mnt
]# find /boot -size +10M -exec ls -lh {} \;
两个条件联合使用
]# mkdir /root/mytab
]# find /etc -name "*tab" -type f
]# find /etc -name "*tab" -type f
-exec cp {} /root/mytab \;
]# ls /root/mytab
利用find查找,数据的所有者为 student,并且必须是文件,把它们拷贝到 /root/findfiles 文件夹中
]# useradd student #创建普通用户student
]# mkdir /root/findfiles
]# find / -user student -type f
]# find / -user student -type f -exec cp {} /root/findfiles \;