格式:find [目录] [条件1]
– 常用条件表示:
-type 类型(f、d、l)
-name "文档名称"
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 修改时间
-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
-name "文档名称"
]# find /etc/ -name "passwd"
]# find /etc/ -name "*tab"
]# find /etc/ -name "*.conf"
]# find /etc/ -name "group"
]# find /etc/ -name "fstab"
]# find /etc/ -name "hosts"
]# find /etc/ -name "shadow"
]# find /etc/ -name "*tab" | wc -l
]# find /etc/ -name "*.conf" | wc -l
]# find /etc/ -name "*.conf" | cat -n
]# wc -l /etc/passwd #统计有多少行
]# cat -n /etc/passwd
[root@A /]# mkdir /mnt/nsd01
[root@A /]# mkdir /mnt/nsd02
[root@A /]# touch /mnt/nsd03.txt
[root@A /]# find /mnt/ -name "nsd*"
[root@A /]# find /mnt/ -name "nsd*" -type d
[root@A /]# find /mnt/ -name "nsd*" -type f
-size +或- 文件大小(k、M、G)
[root@A /]# find /boot/ -size +300k
[root@A /]# find /boot/ -size +10M
[root@A /]# find /boot/ -size +1M
-user 用户名 (按照数据的所有者)
[root@A /]# useradd natasha #创建用户
[root@A /]# find /home/ -user natasha
[root@A /]# find / -user natasha
/proc:内存的数据,不占用硬盘空间
-mtime 修改时间 (所有的时间都是过去时间)
-mtime +90 #90天之前的数据(三个月之前)
-mtime -90 #最近90天之内的数据
[root@A /]# find /root -mtime +90
[root@A /]# find /root -mtime -10
find高级使用
处理find找到的数据,每查找的一个就传递一次
– find [范围] [条件] -exec 处理命令 {} \;
-exec额外操作的开始
{} 永远表示前面find查找的结果
\; 额外操作的结束
]# find /boot/ -size +10M
将/boot目录下大于10M的数据,复制到/mnt
]# find /boot/ -size +10M -exec cp {} /mnt \;
]# ls /mnt/
案例2:查找并处理文件
1. 利用find查找系统中用户 student 拥有的数据,并且必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中
]# useradd student
]# mkdir /root/findfiles
]# find / -user student -type f
]# find / -user student -type f -exec cp {} /root/findfiles \;
]# ls -A /root/findfiles/