find 用法汇总

网友投稿 629 2022-09-23

find 用法汇总

find 用法汇总

本文是本人在工作中的一些find总结,另外一些不常用的find参数汇总,

留个记号以便以后记不住的时候翻一翻

1:查找当前目录下的文件

find . -type f -exec ls -l {  } \;

2:查找当前目录更改日期为5天前的文件,一个+;一个-,分别表示mtime时间为5天前和5天之内

各种参考的使用方式中有用+号,-号,不带符号的用法,那么这里有什么区别

注意这里的n,如果n为带有+号的值,意思为删除n天前所有的文件,比如n=+1且今天是15号,那么删除14号以前的数据,不包括14号,如 果是负号(n=-1)则为删除一天内的文件,比如今天15号,那么删除15号的数据,如果是(n=-2)则代表删除一天前到今天的所有数据,比如今天15 号,那么从14号开始删除。如果不带有符号,那么则删除指定前n天中这一天的数据,比如(n=1)且今天是15号,则删除14号这一天所有数据。  2cto.com

注意这里的一天是指当前系统时间算起的,而不是0-24小时算一天

[root@localhost tmp]# find . -type f -mtime +5  ./.X0-lock [root@localhost tmp]# stat .X0-lock    File: ※.X0-lock-   Size: 11          Blocks: 8          IO Block: 4096   珨啜恅璃 Device: 803h/2051d  Inode: 22839308    Links: 1 Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root) Access: 2012-09-25 09:02:36.000000000 +0800 Modify: 2012-09-25 09:02:36.000000000 +0800 Change: 2012-09-25 09:02:36.000000000 +0800 [root@localhost tmp]#  [root@localhost tmp]# find . -type f -mtime -5  ./test.txt ./test.sh ./crontab.txt ./test/index.html ./0 [root@localhost tmp]# stat test.txt    File: ※test.txt-   Size: 74          Blocks: 8          IO Block: 4096   珨啜恅璃 Device: 803h/2051d  Inode: 22839313    Links: 1 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) Access: 2012-10-24 16:43:57.000000000 +0800 Modify: 2012-10-24 16:44:02.000000000 +0800 Change: 2012-10-24 16:44:02.000000000 +0800

3:perm为权限,

find . -type f -perm 644 -exec ls -l {  } \;

另外find 查找后 都可以交给exec或者 xargs这东西就看你喜好了

size

其中+100k为大于100k -200k为小于200k a为and o为or的 意思

那么第一行的命令意思就是查找当前目录为100k到200k之间的文件并且交给exec ls -lh 打印出来

第二行命令的意思就是大于100k或者小于200k的文件打印出来,个人觉得没意思

[root@localhost ~]# find . -type f -size +100k -a -size -200k  -exec ls -lh {} \; -rw-r--r-- 1 root root 157K 2006-02-13 ./iftop-0.17.tar.gz -rw-r--r-- 1 root root 199K 2011-03-28 ./.scim/pinyin/pinyin_table -rw------- 1 root root 188K 2011-03-28 ./.gstreamer-0.10/registry.x86_64.bin [root@localhost ~]#   find . -type f -size +100k -o -size -200k  -exec ls -lh {} \;

、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。  该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件 ,才能使用下面这个命令  $ find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok rm {  } \; < rm ... ./admin.log001 > ? n < rm ... ./admin.log002 > ? n < rm ... ./admin.log042 > ? n < rm ... ./admin.log942 > ? n

xargs

find 查找文件并且交给file 查看文件类型 [root@localhost ~]# find . -type f -print | xargs file ./.nautilus/metafiles/x-nautilus-desktop:%2F%2F%2F.xml:          XML 1.0 document text ./.nautilus/savedqkzcp7:                                         XML 1.0 document text ./.cshrc:                                                        ASCII text ./b.txt:                                                         empty ./6qu.txt:                                                       UTF-8 Unicode text ./mbox:                                                          UTF-8 Unicode mail text ./22.txt:                                                        ASCII text ./1.sh:                                                          Bourne-Again shell script text executable ./root:                                                          gzip compressed data, from Unix, last modified: Wed Sep 28 17:14:43 2011, max compression ./idctest/idctest_ct.20110423-2000:                              ASCII text ./idctest/idctest_cnc.20110510-2000:                             ASCII text ./idctest/idctest_ct.20110511-1400:                              ASCII text ./idctest/idctest_ct.20110421-1400:                              ASCII text ./idctest/idctest_ct.20110427-0800:                              ASCII text ./idctest/idctest_cnc.20110505-1400:                             ASCII text ./idctest/idctest_cnc.20110503-0800:                             ASCII text ./idctest/idctest_cnc.20110423-0800:                             ASCII text ./idctest/idctest_cnc.20110516-0800:                             ASCII text

忽略某个目录

如果不想在某个目录查找可以忽略掉

find [-path ..] [expression] 在路径列表的后面的是表达式 -path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o -print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。 这个表达式组合特例可以用伪码写为

这个表达式组合特例可以用伪码写为 if -path "/usr/sam"  then           -prune else           -print

[root@localhost tmp]# ls test index.html [root@localhost tmp]# find -name "index.html" -path test -prune   [root@localhost tmp]# find -name "index.html" -path test -prune -o -print../.X0-lock./mapping-root./scim-panel-socket:0-root./test.txt./test.sh./.ICE-unix./.X11-unix./.X11-unix/X0./crontab.txt./.gdm_socket./test./test/index.html./.font-unix./.font-unix/fs7100./0

文件宿主查找

[root@localhost tmp]# find . -user apache  -exec ls -lh {} \; 总计 8.0K -rw-r--r-- 1 apache root 5.4K 10-24 02:00 index.html -rw-r--r-- 1 apache root 5.4K 10-24 02:00 ./test/index.html

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:画图小程序开发「微信小程序做图软件」
下一篇:SQL优化的几种方法(SQL优化的方法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~