Ted's Blog

Happy coding

find

Ted posted @ 2008年8月21日 21:03 in 未分类 with tags find shell , 2315 阅读

Find命令是一个非常有效的工具,它可以遍历当前目录甚至于整个文件系统来查找某些文件或目录。
Find命令的一般形式为:
find pathname -options [-print -exec -ok]
让我们来看看该命令的参数:

 

参数 描述
pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。 
-print find命令将匹配的文件输出到标准输出。
-exec find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为command {} \;,注意{ }和\;之间的空格。
-ok 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

options最常见选项:

option 描述
-name 按照文件名查找文件
-perm 按照文件权限来查找文件
-user 按照文件属主来查找文件
-group 按照文件所属的组来查找文件
-mtime -n +n 按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n 表示文件更改时间距现在n天以前。Find命令还有-atime和-ctime选项,但它们都和-mtime选项相似,所以我们在这里只介绍-mtime选项
-type 查找某一类型的文件,比如:b - 块设备文件;d - 目录;c - 字符设备文件;p - 管道文件;l - 符号链接文件;f - 普通文件
-size n[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计

以下是一些常见实例:

命令 描述
find . -name "*.txt" -print 在当前目录及子目录中查找所有的'*.txt'文件
find . -name "[A-Z]*" -print 当前目录及子目录中查找文件名以一个大写字母开头的文件
find /etc -name "host*" -print 在/etc目录中查找文件名以host开头的文件
find . -name "[a-z][a-z][0--9][0--9].txt" -print 在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是*.txt的文件
find . -size +1000000c -print 在当前目录下查找文件长度大于1 M字节的文件

 

使用exec或ok来执行shell命令
当匹配到一些文件以后,可能希望对其进行某些操作,这时就可以使用-exec选项。一旦find命令匹配到了相应的文件,就可以用-exec选项中的命令对其进行操作(在有些操作系统中只允许-exec选项执行诸如ls或ls -l这样的命令)。大多数用户使用这一选项是为了查找旧文件并删除它们。这里我强烈地建议你在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。
exec选项后面跟随着所要执行的命令,然后是一对儿{ },一个空格和一个\,最后是一个分号

 

用xargs代替exec/ok
在使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给exec执行。不幸的是,有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是"参数列太长"或"参数列溢出"。这就是xargs命令的用处所在,特别是与find命令一起使用。find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

 

以下是一些exec/ok/xargs使用的常见实例:

命令 描述
find logs -type f -mtime +5 -exec rm {} \; 在/logs目录中查找更改时间在5日以前的文件并删除它们
find / -type f -size 0 -exec ls -l {} \; 系统中所有文件长度为0的普通文件,并列出它们的完整路径
find /var/logs -type f -mtime +7 -exec rm {} \; 查找/var/logs目录中更改时间在7日以前的普通文件,并删除它们
find . -name "core" -print | xargs echo "" >/tmp/core.log 在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中
find / -type f -print | xargs grep "device" 用grep命令在所有的普通文件中搜索device这个词

记住,在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!

Avatar_small
Lagend SEO 说:
2020年12月14日 21:02

This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you've done your best, with so much class . If wanted to know more about green smoke reviews, than by all means come in and check our stuff. 123 movie

Avatar_small
전설 서구 说:
2021年1月14日 16:12

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. best canadian online casino

Avatar_small
sophie 说:
2021年2月01日 21:55

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.   Modern kitchen

Avatar_small
asdzxc 说:
2021年7月14日 03:02

Olansi Healthcare Co., Ltd is a professional manufacturer of air purifier, hydrogen water, water purifier, etc. healthcare, more than 12 years experience since 2009 in Guangzhou, China. 60,000 m2 of our own injection molding factory, our own filter factory, our own mold factory, our own assembly factory! 600 square meters of professional laboratory, 30 engineers R&D team. We are prfessional in ODM, OEM services! 3,000 PCs per day of production capacity! 100% aging test for mass production! CE, CB, ROHS, SASO, CQC, CCC approval and ISO 9001: 2008 certificate! Olansi luftreiniger

Avatar_small
Máy lọc không khí Ol 说:
2021年7月17日 19:04

Công ty OLANSI là một nhà sản xuất OEM máy lọc không khí chuyên nghiệp, các sản phẩm có chứa máy lọc không khí Trung Quốc, máy lọc không khí gia đình, máy lọc không khí HEPA, máy lọc không khí ion âm, máy lọc không khí ion hóa, máy lọc không khí PM2.5, máy lọc không khí xe hơi, máy lọc không khí trong phòng, máy lọc không khí thông minh và như thế. Máy lọc không khí Olansi

Avatar_small
n95 说:
2021年7月19日 19:12

This is certainly hence attractive plus artistic. I like a colorations plus whichever company may get them while in the mailbox might be smiling. n95

Avatar_small
asdzxc 说:
2021年8月01日 03:31

Winner of Best of Yolo County 2020 and Best of Weedmaps Kind Farma is Davis California's Premier medical and recreational Cannabis dispensary. kind farms

Avatar_small
celebrity heights 说:
2021年8月04日 18:28

Finally find the info someone asked the other day about Leonard DiCaprio on celeb height wiki I was looking for it all over the web and luckily found it!

Avatar_small
no excuses runner 说:
2021年9月06日 19:51

Whoi, excellent, t wondered just how to cure icne. ind found your webstte by google, dtscovered todiy t'm i ltttle obvtous i greit deil. t’ve ilso idded RSS ind sive your webstte. keep us updited. water heaters near me

Avatar_small
Faddy 说:
2021年9月09日 19:18

Everything has its value. Thanks for sharing this informative information with us. GOOD works! https://allfoodmenuprices.org/

Avatar_small
Faddy 说:
2021年9月16日 20:28

Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. www.storeholidayhours.org

Avatar_small
Faddy 说:
2021年9月18日 19:31 Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. https aka ms remoteconnect code
Avatar_small
Faddy 说:
2021年9月25日 19:30

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also EMF Protection

Avatar_small
SEO 说:
2022年2月28日 23:30

Is anyone needing help in finding the door that you need? I heard a lot of good stuff about Caldwells. https://caldwells.com/ this link if you want to see their products.

Avatar_small
COOK 说:
2022年5月14日 06:15

I am incapable of reading articles online very often, but I’m happy I did today. It is very well written, and your points are well-expressed. I request you warmly, please, don’t ever stop writing. 먹튀검증


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter