find的基本用法
find作用
根据预设的条件递归查询对应的文件
-find [目录] [条件1] [-a|-o][条件2]…
-常用条件表示
find的用法
①-type 类型(f、d、l、b、c、p)文件f,目录d,符号链接文件l,块设备文件b,字符设备文件c,管道文件p
②-name 或 iname 文档名称
③-size +|- 文件大小(K、M、G)
④-user 用户名
⑤-group 组名
⑥-mtime 修改时间
⑦-maxdepth 查询深度
⑧查找文件使用find 命令的-exec操作
举个例子:
①-type 类型
find /boot/ -type f //查找文本文件
find /boot/ -type d //查找目录
find /boot/ -type l //查找快捷方式
②-name 文档名称
find /etc -name "passwd" //查找名为passwd的文档,包括(文件,目录,快捷方式)
find /etc -name "*.conf" // 查找/etc以.conf结尾的文档(不包含子目录)
find /etc/ -name "passwd" -type f //查找/etc下名为passwd的文件
find /etc/ -name "passwd" -o -type f //查找/etc/下名为passwd的文档,或文件(-o选项满足其中一个即可
find /var/ -name "ftp" -type d //查找/var名为ftp的目录
find /etc/ -name "PASSWD" //不忽略大小写查询
find /etc/ -iname "PASSWD" // 忽略大小写查询
③-size +10M 文件大小 (R、M、G)
find /boot/ -size +10M //查找/boot下大于10M的文件
find /boot/ -size -10M //查找/boot小于10M的文件
④-user 用户
find / -user student //查找/下属于student用户的文件
⑤-group 组名
find /home/ -group //查找/home下属于student组的文件
⑥-mtime 根据文件修改时间(都是过去时间)
mtime +10 过去10天之后
-mtime -10 最近10天之内
find /var/log/ -mtime -10 -type f //查询/var/log最近10天之内的日志文件
find /var/log/ -mtime +10 -type f |wc -l //查询/var/log过去10天之后的日志文件并统计日志文件个数
⑦-maxdepth 限制目录查询的深度(最大层数)
find /etc/ -maxdepth 1 -name "*.conf" //限制最大查询层数为1
find /etc/ -maxdepth 2 -name "*.conf" //限制最大查询层数为2
find /etc/ -maxdepth 3 -name "*.conf" //限制最大查询层数为3
⑧find 命令的-exec操作
find … … -exec 处理命令 {};
-优势:以{}代替每一个结果,逐个处理,遇;结束
find /tmp/ -name "hello" -type f -exec cp {} /opt \; //查询/tmp下名为hello的文件并拷贝到/opt目录下
find /boot/ -size +10M -type f -exec cp {} /opt \; //查询/boot下大于10M的文件并拷贝到/opt目录下