1.11.1find基本使用
根据预设的条件递归查找对应的文件
一、格式
find [目录] [条件1]
二、常用选项
-type 类型(f、d、l) //f代表文本文件.d代表目录,l代表快捷方式
-name “文档名称”,严格区分大小写,支持通配符
-size +|-文件大小(k、M、G)
-user 用户名 //按照数据的所有者进行查询
-mtime 按照修改时间查找,所有的时间都是过去的时间
三、用例
[root@localhost03 ~]# find /etc/ -type l //查找etc下的快捷方式
[root@localhost03 ~]# find /etc/ -type d //查找etc下的目录
[root@localhost03 ~]# find /etc/ -type f //查找文件
[root@localhost03 ~]# find /root/ -name "a*"
[root@localhost03 ~]# find /etc/ -name "*tab"
[root@localhost03 ~]# find /root/ -name "nsd*" -type d //多个条件同时使用
[root@localhost03 ~]# find /boot/ -size +10M //查找大于10M的文件
[root@localhost03 ~]# ls -l /home/ //查看用户所有者
total 0
drwx------. 5 tom tom 128 May 25 07:05 tom
[root@localhost03 ~]# find /home/ -name "tom" //查看/home目录下文件所有者是tom的用户
[root@localhost03 ~]# find /root/ -mtime -2 //查找最近两天创建的文件
[root@localhost03 ~]# find /root/ -mtime +90 //查找三个月之前的数据
说明:
容量单位进率
1T=1024G
1G=1024M
1M=1024K
/proc:该目录反应内存的数据,不占用硬盘的空间
1.11.2find高级使用
处理查找的内容
两个参数以上的都不支持管道操作
-find [范围] [条件] -exec 处理命令 {} ;
-exec:额外操作的开始
{}:永远代表find查询的结果,每查找到一个传递一个
:额外操作的结束
[root@localhost03 ~]# find /boot/ -size +10M -exec cp {} /opt/ \; //将/boot下大雨10M的文件和目录,拷贝到/opt目录下
[root@localhost03 ~]# find /etc/ -name "*tab" -exec cp -r {} /opt/ \; //将tab下,以tab结尾的文件和目录,复制到/opt目录下
[root@localhost03 ~]# mkdir /root/findfiles
[root@localhost03 ~]# find / -user student -type f -exec cp {} /root/findfiles/ \; //将所有者为student的文件,复制到/root/findfile目录下