请教个问题

10 views
Skip to first unread message

田飞

unread,
Mar 17, 2013, 7:35:38 AM3/17/13
to linu...@googlegroups.com
对于linux系统文件查找我觉得效率好高,而且可以用通配符来进行快速的查找。
不知道linux文件查找的原理是怎样的,比如 find ./ -name '*.c'  或者 ls m* 之类的命令可以很快列举出目录下匹配的文件。
另外,通配符查找时,系统是怎样去处理的。 

求大牛指导一下,或者提供些资料学习

非常感谢。 

fft fft

unread,
Mar 17, 2013, 7:43:03 AM3/17/13
to linu...@googlegroups.com
我记得""里的和不带任何引号的都是shell给直接展开了?

--
--
To post to this group, send email to linu...@googlegroups.com
website: http://linuxfb.net
twitter: http://twitter.com/linuxfb
IRC: #linux...@irc.freenode.net
slideshare: http://www.slideshare.net/linuxfb
 
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Linux @ BUPT”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 linuxbupt+...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 

田飞

unread,
Mar 17, 2013, 7:55:46 AM3/17/13
to linu...@googlegroups.com
是的。如果没有 引号 “”, shell会去解析。

如果我要查找.c结尾的文件 
find ./ -name '*.c' 的效率好像要快于  
find . -name ‘*' | grep '[.]c$'

嗯。其实我是想知道是不是通配符的效率比正则要高?系统是怎样做到的。


2013/3/17 fft fft <fft...@gmail.com>

fft fft

unread,
Mar 17, 2013, 7:57:26 AM3/17/13
to linu...@googlegroups.com
双引号的也会展开吧?用大量文件的目录time测过了么?我交的第二个慢的原因是因为管道吧?

田飞

unread,
Mar 17, 2013, 8:05:36 AM3/17/13
to linu...@googlegroups.com
我做了个正则查找文件的工具,效率好像比较低。我用系统find命令查找很快。
没有仔细测试。明天上班在大量文件目录下测试以后发结果。
另外,问下展开的意思是说 ”.c“ 并不是由find命令去实现的查找,而是由shell 完成的么?
我有印象: 
 如果大量文件的目录,查找ls *.c 可能会没有响应, 但是 find . -name ”*.c“ 是可以找出来的,我觉得-name "*.c"应该是find去实现的而不是shell去展开的,不知道对不对?

2013/3/17 fft fft <fft...@gmail.com>

fft fft

unread,
Mar 17, 2013, 8:20:11 AM3/17/13
to linu...@googlegroups.com
我错了,貌似是和perl弄混了。。。。。""里的是不被展开的,否则find就该报参数错误了(因为展开了以后有太多的.c文件,都被find当成-name的参数了)

Peng Tao

unread,
Mar 18, 2013, 4:50:44 AM3/18/13
to linu...@googlegroups.com
没看过find的实现,不过直觉它应该是scandir+正则表达式。
你怎么实现的?或许可以share出来大家帮你看看

2013/3/17 田飞 <tian...@gmail.com>:
> --
> --
> To post to this group, send email to linu...@googlegroups.com
> website: http://linuxfb.net
> twitter: http://twitter.com/linuxfb
> IRC: #linux...@irc.freenode.net
> slideshare: http://www.slideshare.net/linuxfb
>
> ---
> 您收到此邮件是因为您订阅了 Google 网上论坛的“Linux @ BUPT”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 linuxbupt+...@googlegroups.com
> 要查看更多选项,请访问 https://groups.google.com/groups/opt_out
>
>



--
Thanks,
Tao

kai li

unread,
Mar 18, 2013, 5:21:04 AM3/18/13
to linu...@googlegroups.com
话说很久木有聚会了
米哥出差出的比在淘宝时还频繁呢
Likai

Xu Wang

unread,
Mar 18, 2013, 5:29:45 AM3/18/13
to linu...@googlegroups.com
前两天网上碰上米哥了,嗯

啥时候聚会一下啊,童鞋们

-- 
Xu Wang

kai li

unread,
Mar 18, 2013, 5:39:50 AM3/18/13
to linu...@googlegroups.com
认识个创新工场的HR 聊天时她说可以提供大家聊天的场地

Xu Wang

unread,
Mar 18, 2013, 5:41:39 AM3/18/13
to linu...@googlegroups.com
缺人缺时间,不缺场地,应该不缺话题,呵呵

-- 
Xu Wang

田飞

unread,
Mar 18, 2013, 7:45:23 AM3/18/13
to linu...@googlegroups.com
我就是用系统api 一个一个文件的去和用户配置的正则比较, 如果遇到目录就递归下去。
初步怀疑,效率低不是文件查找,可能是我返回信息的时候构造的结果比较复杂,导致变慢的。

我比较了一下 find + 通配符 和 scandir + 正则 的方式好像两个速度差不多 。
主要没有很空闲的机器测试。
3次测试时间波动都比较大,很难判断哪个更好。
time find /recent/ -name '*' |grep '.*_2013-03-15_.*' |wc -l
6408

real 0m9.790s
user 0m2.029s
sys 0m1.211s


time find /recent/ -name '*' |grep '.*oos_.*_2013-03-15_.*' |wc -l
6408

real 0m7.637s
user 0m2.012s
sys 0m1.197s

time find /recent/ -name '*oos_*_2013-03-15_*' | wc -l
6408

real 0m7.609s
user 0m1.960s
sys 0m0.488s


2013/3/18 Peng Tao <berg...@gmail.com>

Peng Tao

unread,
Mar 18, 2013, 7:53:57 AM3/18/13
to linu...@googlegroups.com
2013/3/18 田飞 <tian...@gmail.com>:
> 我就是用系统api 一个一个文件的去和用户配置的正则比较, 如果遇到目录就递归下去。
> 初步怀疑,效率低不是文件查找,可能是我返回信息的时候构造的结果比较复杂,导致变慢的。
用的什么API?如果是readdir那就很容易理解了

Peng Tao

unread,
Mar 18, 2013, 7:55:08 AM3/18/13
to linu...@googlegroups.com
啥时候聚聚吧,好久不见,甚是想念呀~~kakaka

2013/3/18 Xu Wang <gna...@gmail.com>:

Peng Tao

unread,
Mar 18, 2013, 7:56:45 AM3/18/13
to linu...@googlegroups.com
2013/3/18 kai li <likai...@gmail.com>:
> 话说很久木有聚会了
> 米哥出差出的比在淘宝时还频繁呢
贵司公务繁忙啊

Hu.Ziming

unread,
Mar 18, 2013, 10:13:42 AM3/18/13
to linu...@googlegroups.com
嗯,想念+65536~

2013/3/18 Peng Tao <berg...@gmail.com>:

Peng Wu

unread,
Mar 18, 2013, 10:44:49 PM3/18/13
to linu...@googlegroups.com
由于磁盘缓存的原因,一般后来的结果会比第一次快。

在 2013-03-18一的 19:45 +0800,田飞写道:
> 我就是用系统api 一个一个文件的去和用户配置的正则比较, 如果遇到目录就
> 递归下去。
> 初步怀疑,效率低不是文件查找,可能是我返回信息的时候构造的结果比较复
> +unsub...@googlegroups.com
> 要查看更多选项,请访问 https://groups.google.com/groups/opt_out
>
>


田飞

unread,
Mar 19, 2013, 12:18:37 AM3/19/13
to linu...@googlegroups.com
有什么办法能够排除缓存,看到底哪个效率高呢?

2013/3/19 Peng Wu <peng...@gmail.com>

Eryu Guan

unread,
Mar 19, 2013, 12:23:02 AM3/19/13
to linu...@googlegroups.com
2013/3/19 田飞 <tian...@gmail.com>:
> 有什么办法能够排除缓存,看到底哪个效率高呢?

echo 3 > /proc/sys/vm/drop_caches
这个应该行
Reply all
Reply to author
Forward
0 new messages