【问题标题】:directory-first-search with find使用 find 进行目录优先搜索
【发布时间】:2016-11-27 22:25:24
【问题描述】:

find 迭代目录时,它们会显示在the order the VFS yields them 中。这个顺序可以改成先遍历目录再查看放在旁边的文件吗?

-depth 选项不是解决方案。它只会改变

$ find
.
./afile
./directory
./directory/athirdfile
./other-directory

$ find -depth
./afile
./directory/athirdfile
./directory
./other-directory

(注意只有第二条和第三条输出线是如何交换位置的。)

这个问题反而寻求一种产生以下顺序的方法。

./directory/athirdfile
./directory
./other-directory
./afile

【问题讨论】:

    标签: shell gnu-findutils find-util


    【解决方案1】:

    您可以使用 2 个find 命令获得自定义输出。第一个find 将使用-depth 获取除当前目录中的文件之外的所有内容,第二个find 将仅获取当前级别的文件。

    { find . -depth -mindepth 1; find . -maxdepth 1 -depth -type f; }
    

    【讨论】:

    • 一种很有前途的方法,但它还不能递归地工作。子目录也应该出现在放在它们旁边的文件之前。不过,通过-exec 将两个finds 链接在一起可以工作。
    • 在我的 gnu find 子目录确实出现在起始目录的文件之前
    • 在我的系统上,这是一个 GNU 通过内核 4.6.4 上的 tmpfs 查找,mkdir -p a/b/ctouch a/d,然后是 { find . -depth -mindepth 1; find . -maxdepth 1 -depth -type f; } | tr '\n' , 输出 ./a/d,./a/b/c,./a/b,./a,。文件a/d 出现在同级目录a/b 之前。
    • 完全相同的设置和相同的命令给我./a/b/c,./a/b,./a/d,./a, 输出。我正在使用find (GNU findutils) 4.6.0
    • 我在 Ubuntu 上使用find (GNU findutils) 4.4.2 测试了相同的命令集,得到了相同的输出,即./a/b/c,./a/b,./a/d,./a,
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2021-07-12
    • 2022-01-02
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多