shell find命令使用

语法格式

find [路径] [选项] [操作]


选项:
-name 【按照文件名称查找】
find /etc -name '*.conf'  //查找/etc目录下以conf结尾的文件

-iname 
find . -iname aa //  查找当前目录下文件名为aa的文件,不区分大小写
-perm 【按照权限查找:-perm 755】
finc . -perm 777   //查找当前目录下权限是777的文件
-prune 【使用此选项可以使find命令不在当前指定的目录中查找】
通常和-path一起使用,用于将特定目录排除在搜索条件之外
例子1:查找当前目录下所有普通文件,但排除test目录
find . -path ./test -prune -o -type f
例子2:查找当前目录下所有普通文件,但排除etc和opt目录
find . -path ./etc -prune -o -path ./opt -prune -o -type f
-user 【按照文件属主来查找】
find . -user wangteng   //查找文件属主为wangteng的所有文件
-group【按照文件属组来查找】
find . -group wangteng  //查找属组为wangteng的当前目录所有文件
-type 【查找某一类型的文件】
           type的选项:
           b 【块设备文件】
           d 【目录】
           c 【字符设备文件】
           f 【普通文件】
           p 【管道文件】
           l 【符号链接文件】
-amin n 【查找系统中最后n分钟访问的文件】
-cmin n 【查找系统中最后n分钟被改变文件状态的文件】
-ctime n【查找系统中最后n*24小时{即n天前}被改变文件状态的文件】
-mmin n 【查找系统中最后n分钟被改变文件数据的文件】
find /etc -mmin +30  //查找/etc目录下30分钟之前修改的文件
find /etc -mmin 30 -type d   、/查找etc目录下30分钟之内修改的目录
-empty              查找为空的文件
-size               按容量大小查找
find /etc -size -10000c  //查找/etc目录下小雨10000字节的文件
find /etc -size +1M   //查找/etc目录下大与1M的文件
-perm mode          查找指定属性的文件
-mtime N            查找在N天前被修改过的文件【查找系统中最后n*24小时{即n天前}被改变文件数据的文件】 +n:N天前find /var -mtime +3;-n:N天内 

find /etc -mtime -5 -name '*.conf'  //查找/etc目录下5天内修改过且以conf结尾的文件
find /etc -mtime +10 -user root  //查找/etc目录下十天之前修改且属主为root的文件

-atime N            查找在N天前被访问过的文件【查找系统中最后n*24小时{即n天前}访问的文件】
-exec   对搜索到的文件执行特定的操作,格式为-exec 'command' {} ;
例子1:搜索/etc下的文件(非目录),文件名以conf结尾,且大于10K,然后将其删除
find /etc/ -type f -name '*.conf' -size +10k -exec rm -f {} ;
例子2:将/var/log/目录下以log结尾的文件,且更新时间在7天以上的删除
find /var/log/ -name '*.log' -mtime +7 -exec rm -rf {} ;
例子3:搜索条件和例子1一样,只是不删除,而是将其复制到/root/conf目录下
find /etc/ -size 10k -type f -name '*.conf' -exec cp {} /root/conf ;
find . ( -name "*.txt" -o -name "*.pdf" ) -print   //查找txt和pdf文件

find . -name "[a-a]*"  //查找所有字母开头的文件

find . ! -name "*.txt" -print //否定参数->查找所有非txt文本

find . -maxdepth 1 -type f  //指定搜索深度->打印出当前目录的文件(深度为1)

find . -regex  ".*(.txt|.pdf)$"  //正则方式查找.txt和pdf

find . -type f -size +2k  //寻找大于2k的文件

find . -type f -name "*.avi" -delete  //删除当前目录下所有的avi文件

find . -type f -user root -exec chown root {} ; //将当前目录下的所有权变更为root 执行动作(强大的exec)
原文地址:https://www.cnblogs.com/wt645631686/p/8472783.html