一、find的作用?
•递归式查找
•根据预设的条件递归查找对应的文件
二、find使用方法
– find [目录] [条件1]…
-type 类型(f文本文件、d目录、l快捷方式)
代码如下:
[root@localhost ~]# find /home -type f
[root@localhost ~]# find /root -type f
[root@localhost ~]# find /home -type d
[root@localhost ~]# find /root -type d
[root@localhost ~]# find /etc -type l
-name “名称”
代码如下:
[root@localhost ~]# find /etc/ -name "*.conf"
[root@localhost ~]# find /etc/ -name "*tab*"
[root@localhost ~]# find /etc/ -name "passwd"
-size +或-文件大小(k、M、G)
[root@localhost ~]# find /boot/ -size +20M
[root@localhost ~]# find /boot/ -size -10M
[root@localhost ~]# find /boot/ -size +300k
-user 用户名 #按照数据的所有者
[root@localhost ~]# useradd wxr #新建用户
[root@localhost ~]# find /home -user zhangsan
-mtime 修改时间 #都是过去时间
三个月之前的数据:
[root@localhost ~]# find /root -mtime +90
查找最近一天内:
[root@localhost ~]# find /root -mtime -1
三、find高级使用处理查找的内容
案例:查找并处理文件
1. 利用find查找所有用户 student 拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中
[root@localhost ~]# useradd student
[root@localhost ~]# mkdir /root/findfiles
[root@localhost ~]# find / -user student -type f
[root@localhost ~]# find / -user student -type f -exec cp {
} /root/findfiles/ \;
[root@localhost ~]# ls -A /root/findfiles/
• 操作方法:
find [范围] [条件] -exec 处理命令 {} ;
– -exec :额外操作开始
– {} :find查询的结果
– ; :额外操作结束
– 每找到一个符合条件的数据,直接传输给-exec,每次只传输一个参数