Ted's Blog

Happy coding

find

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

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. 먹튀검증

Avatar_small
COOK 说:
2022年5月28日 19:21

Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as ABC. https://www.ubank789.com

Avatar_small
COOK 说:
2022年5月28日 23:06

Thank you for very usefull information.. baccarat online

Avatar_small
COOK 说:
2022年5月30日 22:21

I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. www.ibizaclub88.com

Avatar_small
COOK 说:
2022年5月30日 23:46

If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content. ESL grammar conditionals lesson plan

Avatar_small
COOK 说:
2022年5月31日 20:48

Thanks for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing? www.betflikdc.com

Avatar_small
COOK 说:
2022年5月31日 23:55

i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. leedon green

Avatar_small
COOK 说:
2022年6月01日 18:57

This blog is really great. The information here will surely be of some help to me. Thanks!. http://www.mawin77.bet

Avatar_small
COOK 说:
2022年6月01日 23:29

Great knowledge, do anyone mind merely reference back to it weighted blanket benefits

Avatar_small
COOK 说:
2022年6月03日 20:26

I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. lcctoto

Avatar_small
COOK 说:
2022年6月09日 23:57

Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. Büro an untermieter vermieten

Avatar_small
COOK 说:
2022年6月14日 19:02

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. Sofa reupholstery

Avatar_small
COOK 说:
2022年6月14日 21:52 Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. Haus kaufen in Werlte
Avatar_small
COOK 说:
2022年6月15日 17:53

There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. Thank you for your clear information. peak residence condo

Avatar_small
COOK 说:
2022年6月19日 16:52

Thanks for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing? 먹튀검증커뮤니티

Avatar_small
COOK 说:
2022年7月05日 20:41

i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me. Save Money

Avatar_small
COOK 说:
2022年7月17日 16:42

Thankyou for this wondrous post, I am glad I observed this website on yahoo. this chateau for sale in France

Avatar_small
COOK 说:
2022年7月18日 20:08

Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting. https://designacademy.bg/index.php?q=education&course=119

Avatar_small
Harry 说:
2022年7月19日 18:34

Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet! subaru sls

Avatar_small
COOK 说:
2022年7月24日 01:26 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. https://www.ccrgdlaw.com/
Avatar_small
COOK 说:
2022年7月25日 20:53

This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great. https://designacademy.bg/index.php?q=question&id=1471&ask=Могат_ли_интериорните_дизайнери_да_бъдат_богати

Avatar_small
COOK 说:
2022年7月28日 22:12

Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. credit cards

Avatar_small
dark web/deep web/d 说:
2022年8月04日 18:52

There is nothing to stop someone from creating thousands of fake links and advertising them on the Internet.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:37

Some sites also contain embedded images and video, which users often do not want to open and view. In addition, many of these dark web links contain a "pay-per-click" advertising program that allows advertisers to display their ads right in front of millions of web users.  deep web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:52

By being cautious about where you post your information and making sure that sites that you visit do not contain harmful embedded scripts, you can reduce your risk of becoming a victim.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:14

There are many commonalities in these links as well. Some of them are link farms, automated websites, spam links, and phishing scams. There are a few other things as well.   dark web sites

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:29

You also need to be aware of the fact that when you are looking at these links, you are going to get a lot of information on the site. Some of it might be worth reading, and some could be harmful. So just be careful who you associate with and don't give out credit card numbers online! But you can enjoy the dark web if you learn how to work around the dangers.  dark web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:44

Affiliate marketing is all about finding the right product to market, and you have to focus on the right niche in order to make any money at all.   work from home jobs

Avatar_small
dark web/deep web/d 说:
2022年8月04日 21:02

The series went so well that it earned the company $4 million in sales in its first three months. The secret to their affiliate marketing success stories?  affiliate marketing success

Avatar_small
COOK 说:
2022年10月01日 16:18 Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles . 토토커뮤니티
Avatar_small
COOK 说:
2022年10月16日 21:22

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! mtp kit uses

Avatar_small
COOK 说:
2022年10月18日 05:07 Your work is truly appreciated round the clock and the globe. It is incredibly a comprehensive and helpful blog. instagram auto liker
Avatar_small
COOK 说:
2022年10月19日 01:44 Great knowledge, do anyone mind merely reference back to it mosman fencing
Avatar_small
Harry 说:
2022年10月25日 23:32

i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me. asianbookie bola

Avatar_small
Harry 说:
2022年11月01日 16:35

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back. spbo live score indonesia

Avatar_small
Harry 说:
2022年11月16日 03:04

Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. unicc at

Avatar_small
COOK 说:
2022年11月16日 20:15

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. gossipsecter

Avatar_small
click here 说:
2022年11月25日 03:40

You could certainly see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always go after your heart.

Avatar_small
COOK 说:
2022年12月31日 18:54

I can’t imagine focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material. This is great content. Quad Biking

Avatar_small
COOK 说:
2023年1月04日 21:26

If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you. Quad Biking

Avatar_small
backlinks 说:
2023年2月15日 16:58

I appreciate you taking the time to talk about them with people. <a href="https://www.fiverr.com/share/8eK3Zq">backlinks</a>

Avatar_small
backlinks 说:
2023年2月15日 16:58

I appreciate you taking the time to talk about them with people. backlinks

Avatar_small
guest post service 说:
2023年2月18日 18:10

Hmmm, I am not therefore ‘ consider every little thing the following, but you do offer a important information with this make a difference. I’ll instruct me personally plus revisit at a later date. guest post service

Avatar_small
jsimitseo 说:
2024年1月30日 22:30

I can suggest essentially not too bad and even dependable tips, accordingly see it:  best concierge doctor


登录 *


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