使用 find 命令搜索直到第一个匹配项

问题描述 投票:0回答:1

我只需要搜索可以在任何地方的特定目录,有没有办法运行此命令直到第一个匹配?谢谢! 我现在正在使用

find / -noleaf -name 'experiment' -type d | wc -l
bash command find
1个回答
5
投票

正如 Rudolf Mühlbauer 提到的,

-quit
选项告诉
find
退出。手册页示例是这样的
find /tmp/foo /tmp/bar -print -quit

仅打印
/tmp/foo

更一般地说,

find
可能是你想做的事情的错误工具。请参阅
man locate
。例如,在我的系统上,
locate experiment | head -3

产生

/usr/lib/tc/experimental.dist
/usr/share/doc/xorg/reference/experimental.html
/usr/share/doc/xorg/reference/experimental.txt

同时

locate -r 'experimental..$'
生成(为简洁起见,剪掉了 6 行)

/usr/src/linux-headers-3.2.0-24-generic/include/config/experimental.h
 (snip)
/usr/src/linux-headers-3.2.0-32-generic/include/config/experimental.h

正如ShpielMeister 的评论中所指出的,

locate
数据库仅定期更新(例如,在Ubuntu Linux 系统上每天更新)。当需要关注新文件时,请说
sudo updatedb
,一两秒钟的工作就会更新数据库。另请参阅:ioflood.com 关于定位命令的博客

© www.soinside.com 2019 - 2024. All rights reserved.