find命令的exec选项
在用find命令查找到我们需要的目标以后,我们经常要继续对找到的目标文件进行处理,exec选项就为我们提供了这样的方便。[b][indent]删除当前目录(包括子目录)下的所有.o文件[/indent][/b]
[indent][code]find "$PWD" -type f -name '*.o' -exec rm {} \;[/code][/indent]
[indent]"find"找到的.o文件将会逐个将名字将替换到{}的位置,"\"是保证";"被正确转义[/indent]
[b][indent]把当前目录(包括子目录)下所有的.h文件移动到上一级的include目录中[/indent][/b]
[indent][code]find "$PWD" -type f -name '*.h' -exec mv {} ../include/ \;[/code][/indent]
自己怎么应用,慢慢琢磨吧。 find "$PWD" -type f -name '*.o' | rm -rf;
也可以达到同样效果 建议楼上最好把rm -rf 改为rm -f ,
养成这样的习惯, 我后悔了好多次! [quote]原帖由 [i]Flyinmorning[/i] 于 2008-5-9 13:33 发表 [url=http://bbs.linuxpk.com/redirect.php?goto=findpost&pid=49869&ptid=14374][img]http://bbs.linuxpk.com/images/common/back.gif[/img][/url]
find "$PWD" -type f -name '*.o' | rm -rf;
也可以达到同样效果 [/quote]
在.o文件数量不是很大的情况下的确效果相同,但如果.o的数量非常大的话,超过rm的参数长度就不能运行了。反正在我工作的工程目录下是不行的。管道是把前面的结果一起送到后面做参数,而exec是一个一个的送过去。 善用xargs
[root@monitoring openssh-4.7p1]# pwd
/usr/local/src/openssh-4.7p1
time find "$PWD" -type f -name '*.h' -exec ls {} \;
real 0m0.102s
user 0m0.028s
sys 0m0.080s
time find "$PWD" -type f -name "*.h" |xargs ls
real 0m0.006s
user 0m0.002s
sys 0m0.005s
利用xargs 效率高 [quote]原帖由 [i]NetSeek[/i] 于 2008-5-10 08:34 发表 [url=http://bbs.linuxpk.com/redirect.php?goto=findpost&pid=49923&ptid=14374][img]http://bbs.linuxpk.com/images/common/back.gif[/img][/url]
利用xargs 效率高
[/quote]
xargs也解决了了参数过长的问题,透过管道调用,效率反而提高不少。赞一个! xargs 不错。
通常我用 xargs 和 find 配合的时候都是用[code]find ... -print0 | xargs -0 -r ....[/code]避免文件名里面有空格造成麻烦。
BTW,这是 man page 里面的推荐用法。 [quote]原帖由 [i]NetSeek[/i] 于 2008-5-10 08:34 发表 [url=http://bbs.linuxpk.com/redirect.php?goto=findpost&pid=49923&ptid=14374][img]http://bbs.linuxpk.com/images/common/back.gif[/img][/url]
善用xargs
[root@monitoring openssh-4.7p1]# pwd
/usr/local/src/openssh-4.7p1
time find "$PWD" -type f -name '*.h' -exec ls {} \;
real 0m0.102s
user 0m0.028s
sys 0m0.080s
time find ... [/quote]
我建议 [font=monospace]find "$PWD"[/font] 写成 [font=monospace]find .[/font] 就可以了。这样比较简洁。 受益了 很受用, 很喜欢 顶个!嘿嘿! 恩. . .. 这样的确可以. . .
而且还很简便。 . . . ..
支持. . . .. 举个例子,作为对6楼的说明
假设目录下有a b c 三个文件,a中内容是b c
执行:
tux@tux-desktop:~/lzp$ cat a | xargs --verbose --max-args=1 ls -l
ls -l b
-rw-r--r-- 1 tux tux 0 2008-07-05 10:57 b
ls -l c
-rw-r--r-- 1 tux tux 0 2008-07-05 10:57 c 明白一点了,呵呵:victory: find . -type f -name "*.php" | xargs grep -i "my String" 现在我的一个文件夹随着时间会越来越多,用rm -rf 会超过长度,于是用:
find . -name "*20090105*" -exec rm -rf {} \;
不过这个方法太慢了
删除的小文件太慢了。
一个文件夹中小文件应该有5000千以上,并随着时间的增多,而增多。
各位同行,有没有好的方法进行删除? exec是不开另外进程执行的,在某些时候用效果不错 YC54YC新手还看不懂啥意思 ,相信以后能。 学到很多东西。
页:
[1]