GNU find:首先在当前目录中搜索
如何告诉 find 首先在当前文件夹中查找,然后继续在子文件夹中搜索?我有以下内容:
$ find . -iname '.note'
folder/1/.note
folder/2/.note
folder/3/.note
folder/.note
我想要的是:
$ find . -iname '.note'
folder/.note
folder/1/.note
folder/2/.note
folder/3/.note
有什么想法吗?
how can I tell find to look in the current folder first and then continue search in subfolders? I have the following:
$ find . -iname '.note'
folder/1/.note
folder/2/.note
folder/3/.note
folder/.note
What I want is this:
$ find . -iname '.note'
folder/.note
folder/1/.note
folder/2/.note
folder/3/.note
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
find
的算法如下:对于
-depth
主,步骤 1 和 2 以相反的顺序执行。您要求
find
做的是在步骤 2 中先考虑文件,然后再考虑目录。但是find
没有选项可以做到这一点。在您的示例中,匹配文件的所有名称都位于同一目录中的子目录名称之前,因此
find 。 -iname '.note' | sort
会起作用。但这显然不能很好地概括。在目录之前处理非目录的一种方法是运行
find
命令来迭代目录,并运行一个单独的命令(可能再次使用find
)来打印该目录中的匹配文件。如果您想使用
find
表达式来匹配文件,这里是第二个find
命令的一般结构,用于仅以非递归方式迭代指定目录中的非目录(需要GNU find):例如:
在zsh中,可以将
**/
递归写入子目录;(#i)
(通配标记 ) 将后面的内容解释为不区分大小写的模式,并且(Od)
(glob 限定符)对递归遍历的结果进行排序,以便在子目录之前考虑目录中的文件。使用(Odon)
,输出在Od
规定的约束内按字典顺序排序(即主要排序标准排在第一位)。find
's algorithm is as follows:With the
-depth
primary, steps 1 and 2 are executed in the opposite order.What you're asking
find
to do is to consider files before directories in step 2. Butfind
has no option to do that.In your example, all names of matching files come before names of subdirectories in the same directory, so
find . -iname '.note' | sort
would work. But that obviously doesn't generalize well.One way to process non-directories before directories is to run a
find
command to iterate over directories, and a separate command (possiblyfind
again) to print matching files in that directory.If you want to use a
find
expression to match files, here's a general structure for the secondfind
command to iterate only over non-directories in the specified directory, non-recursively (GNU find required):For example:
In zsh, you can write
**/
recurses into subdirectories;(#i)
(a globbing flag) interprets what follows as a case-insensitive pattern, and(Od)
(a glob qualifier) orders the outcome of recursive traversals so that files in a directory are considered before subdirectories. With(Odon)
, the output is sorted lexicographically within the constraint laid out byOd
(i.e. the primary sort criterion comes first).解决方法是
find 。 -iname '.note' | sort -r :
但是在这里,输出只是以相反的顺序排序,这不会改变 find 的行为。
Workaround would be
find . -iname '.note' | sort -r
:But here, the output is just sorted in reverse order and that does not change find's behaviour.
对于我来说,在 Linux 上使用 GNU find 时,我得到了具有不同测试运行的两个订单。
测试用例:
通过这个测试,我得到了发布者的第一个结果。请注意
1 2 3 .
的顺序。如果我将此顺序更改为. 1 2 3
我得到了发布者的第二个结果。
在任何一种情况下,添加 -depth 来查找都不起作用。
编辑:
我写了一个 perl oneliner 来进一步研究这一点:
在运行测试用例 1 后,我对 /tmp/depthtest 运行了这个,结果如下:
在测试用例 2 后,我再次运行它,结果如下:
这确认了结果位于目录中命令。
find 的
-depth
选项仅控制./1/.note
是在./1/
之前还是之后处理,而不是控制 < code>./.note 或 ./1/
是第一个,因此结果的顺序纯粹基于目录顺序(主要是创建顺序)。查看如何进行可能会有所帮助我是否以广度优先的方式递归列出某个位置的所有目录? 以了解如何解决此问题。
For me with GNU find on Linux I get both orderings with different test runs.
Testcase:
With this test I get the poster's first result. Note the ordering of
1 2 3 .
. If I alter this ordering to to. 1 2 3
I get the poster's second result.
In either case adding -depth to find does nothing.
EDIT:
I wrote a perl oneliner to look in to this further:
And I ran this against /tmp/depthtest after running testcase 1 with these results:
I ran it again after testcase 2 with these results:
Which confirms that the results are in directory order.
The
-depth
option to find only controls whether e.g../1/.note
is processed before or after./1/
, not whether./.note
or ./1/
is first, so the order of the results is purely based on directory order (which is mostly creation order).It might be helpful to look at How do I recursively list all directories at a location, breadth-first? to learn how to work around this problem.
查找 -s 。 -iname ".note" 没有帮助吗?或
查找 . -iname '.note'|排序
?find -s . -iname ".note"
doesn't help? orfind . -iname '.note'|sort
?在当前文件夹中查找
==>73!
Find in the current folder
==>73!