GNU find:首先在当前目录中搜索

发布于 2024-10-03 07:35:52 字数 296 浏览 1 评论 0原文

如何告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙女山的月亮 2024-10-10 07:35:52

find 的算法如下:

  • 对于命令行上给出的每个路径,让当前条目为该路径,然后:
    1. 将当前条目与表达式进行匹配。
    2. 如果当前条目是一个目录,则按未指定的顺序对该目录中的每个条目执行步骤 1 和 2。

对于 -depth 主,步骤 1 和 2 以相反的顺序执行。

您要求 find 做的是在步骤 2 中先考虑文件,然后再考虑目录。但是 find 没有选项可以做到这一点。

在您的示例中,匹配文件的所有名称都位于同一目录中的子目录名称之前,因此 find 。 -iname '.note' | sort 会起作用。但这显然不能很好地概括。

在目录之前处理非目录的一种方法是运行 find 命令来迭代目录,并运行一个单独的命令(可能再次使用 find)来打印该目录中的匹配文件。

find -type d -exec print-matching-files-in {} \;

如果您想使用 find 表达式来匹配文件,这里是第二个 find 命令的一般结构,用于仅以非递归方式迭代指定目录中的非目录(需要GNU find):

find -type d -exec find {} -maxdepth 1 \! -type d … \;

例如:

find -type d -exec find {} -maxdepth 1 \! -type d -iname '.note' \;

在zsh中,可以将

print -l **/(#i).note(Od)

**/递归写入子目录; (#i)通配标记 ) 将后面的内容解释为不区分大小写的模式,并且 (Od)glob 限定符)对递归遍历的结果进行排序,以便在子目录之前考虑目录中的文件。使用(Odon),输出在Od 规定的约束内按字典顺序排序(即主要排序标准排在第一位)。

find's algorithm is as follows:

  • For each path given on the command line, let the current entry be that path, then:
    1. Match the current entry against the expression.
    2. If the current entry is a directory, then perform steps 1 and 2 for every entry in that directory, in an unspecified order.

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. But find 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 (possibly find again) to print matching files in that directory.

find -type d -exec print-matching-files-in {} \;

If you want to use a find expression to match files, here's a general structure for the second find command to iterate only over non-directories in the specified directory, non-recursively (GNU find required):

find -type d -exec find {} -maxdepth 1 \! -type d … \;

For example:

find -type d -exec find {} -maxdepth 1 \! -type d -iname '.note' \;

In zsh, you can write

print -l **/(#i).note(Od)

**/ 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 by Od (i.e. the primary sort criterion comes first).

橘虞初梦 2024-10-10 07:35:52

解决方法是 find 。 -iname '.note' | sort -r :

$ find . -iname '.note' | sort -r
folder/.note
folder/3/.note
folder/2/.note
folder/1/.note

但是在这里,输出只是以相反的顺序排序,这不会改变 find 的行为。

Workaround would be find . -iname '.note' | sort -r:

$ find . -iname '.note' | sort -r
folder/.note
folder/3/.note
folder/2/.note
folder/1/.note

But here, the output is just sorted in reverse order and that does not change find's behaviour.

秋日私语 2024-10-10 07:35:52

对于我来说,在 Linux 上使用 GNU find 时,我得到了具有不同测试运行的两个订单。

测试用例:

rm -rf /tmp/depthtest ; mkdir -p /tmp/depthtest ; cd /tmp/depthtest ; for dir in 1 2 3 . ; do mkdir -p $dir ; touch $dir/.note ; done ; find . -iname '.note'

通过这个测试,我得到了发布者的第一个结果。请注意 1 2 3 . 的顺序。如果我将此顺序更改为 . 1 2 3

rm -rf /tmp/depthtest ; mkdir -p /tmp/depthtest ; cd /tmp/depthtest ; for dir in . 1 2 3 ; do mkdir -p $dir ; touch $dir/.note ; done ; find . -iname '.note'

我得到了发布者的第二个结果。

在任何一种情况下,添加 -depth 来查找都不起作用。

编辑:

我写了一个 perl oneliner 来进一步研究这一点:

perl -e 'opendir(DH,".") ; print join("\n", readdir(DH)),"\n" ; closedir(DH)'

在运行测试用例 1 后,我对 /tmp/depthtest 运行了这个,结果如下:

.
..
1
2
3
.note

在测试用例 2 后,我再次运行它,结果如下:

.
..
.note
1
2
3

这确认了结果位于目录中命令。

find 的 -depth 选项仅控制 ./1/.note 是在 ./1/ 之前还是之后处理,而不是控制 < code>./.note 或 ./1/ 是第一个,因此结果的顺序纯粹基于目录顺序(主要是创建顺序)。

查看如何进行可能会有所帮助我是否以广度优先的方式递归列出某个位置的所有目录? 以了解如何解决此问题。

For me with GNU find on Linux I get both orderings with different test runs.

Testcase:

rm -rf /tmp/depthtest ; mkdir -p /tmp/depthtest ; cd /tmp/depthtest ; for dir in 1 2 3 . ; do mkdir -p $dir ; touch $dir/.note ; done ; find . -iname '.note'

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

rm -rf /tmp/depthtest ; mkdir -p /tmp/depthtest ; cd /tmp/depthtest ; for dir in . 1 2 3 ; do mkdir -p $dir ; touch $dir/.note ; done ; find . -iname '.note'

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:

perl -e 'opendir(DH,".") ; print join("\n", readdir(DH)),"\n" ; closedir(DH)'

And I ran this against /tmp/depthtest after running testcase 1 with these results:

.
..
1
2
3
.note

I ran it again after testcase 2 with these results:

.
..
.note
1
2
3

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.

蓝戈者 2024-10-10 07:35:52

查找 -s 。 -iname ".note" 没有帮助吗?或 查找 . -iname '.note'|排序 ?

find -s . -iname ".note" doesn't help? or find . -iname '.note'|sort ?

倒带 2024-10-10 07:35:52

在当前文件夹中查找

 find ./in_save/ -type f -maxdepth 1| more

==>73!

Find in the current folder

 find ./in_save/ -type f -maxdepth 1| more

==>73!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文