码迷,mamicode.com
首页 > 系统相关 > 详细

Linux find命令详解

时间:2018-07-01 22:06:39      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:内容   http   var   更改   shell   medium   html   name   状态   

Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大

find常见命令参数

命令选项:
-name   按照文件名查找文件。
-perm   按照文件权限来查找文件。
-user   按照文件属主来查找文件。
-group  按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件 【-7 7天之内 +7 7天前】
-nogroup  查找无效属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser  查找无效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type  查找某一类型的文件,诸如:
         b - 块设备文件。
         d - 目录。
         c - 字符设备文件。
         p - 管道文件。
         l - 符号链接文件。
         f - 普通文件。
-size n:[c] 查找文件长度为n块的文件,带有c表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。

另外,下面三个的区别:
-amin n    查找系统中最后N分钟访问的文件
-atime n   查找系统中最后n*24小时访问的文件
-cmin n    查找系统中最后N分钟被改变文件状态的文件
-ctime n   查找系统中最后n*24小时被改变文件状态的文件
-mmin n    查找系统中最后N分钟被改变文件数据的文件
-mtime n   查找系统中最后n*24小时被改变文件数据的文件

常用的命令展示

删除目录下所有文件

find /tmp/ -type f -exec rm -rf {} \;
find /tmp/ -type f | xargs rm -rf

查看当前路径下所有文件的信息:

find /tmp/ -type f ! -name a |xargs rm –rf
find ./ -type f -exec file {} \;

 

查找指定时间内修改过的文件

# 当前路径下访问文件超过2分钟文件
find ./ -amin +2
# 当前路径下访问文件刚好2分钟的文件
find ./ -amin 2
find ./ -cmin +2
find ./ -mmin +2
find ./ -mtime +2
find ./ -ctime +2
find ./ -mtime +2
find ./ -ctime +2 

按照目录或文件的权限来查找文件

find /opt -perm 777

按大小查找文件

find / -size +10M  |sort 【查找大于10M的文件】
find / -size -10M  |sort 【查找小于10M的文件】
find / -size 10M   |sort  【查找10M的文件】

 

在test 目录下查找不在test4子目录之内的所有文件

find ./test -path "test/test4" -prune -o -print
【可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略】

查找比yum.log 但不比hhh.txt新的文件

[root@localhost ftl]# find / newer /var/log/yum.log ! -newer ./hhh.txt

 

查找更改时间在比log2012.log文件新的文件

find ./ -newer log2012.log

在当前目录下查找文件长度大于1 M字节的文件

find ./ -size +1000000c –print
find ./ –size +1M -print

在/home/apache目录下查找文件长度恰好为100字节的文件:

find /home/apache -size 100c -print

在当前目录下查找长度超过10块的文件

find . -size 10 -print

find命令之exec/ok/print

ls -l命令放在find命令的-exec选项中

find . -type f -exec ls -l {} \; 【{}   花括号代表前面find查找出来的文件名】

在目录中查找更改时间在n日以前的文件并删除它们

 find ./ -mtime +10 -exec rm {} \;

在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示

find / -mtime +1 -a -name "*.log" -type f -ok cp {} /tmp/ftl \; 【-ok是安全模式,根exec效果同】

-exec中使用grep命令

find /etc -name "passwd*" -exec grep "root" {} \; 【过滤文件内容用】

查找文件移动到指定目录

find . -name "*.log" -exec mv {} .. \;

用exec选项执行cp命令  

find . -name "*.log" -exec cp {} test3 \;

 

find命令xargs

Linux xargs命令详解

https://www.cnblogs.com/ftl1012/p/9250438.html

 

Linux find命令详解

标签:内容   http   var   更改   shell   medium   html   name   状态   

原文地址:https://www.cnblogs.com/ftl1012/p/9251300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!