列出所有空文件夹

列出所有空文件夹

我有一堆嵌套文件夹。大多数文件夹都包含文件。有些文件夹包含数十万个文件。有些文件夹是空的。

我想获取所有空文件夹的列表。但是,当我运行:

find -type d -empty

它需要一个非常运行时间很长,比仅仅运行所需的时间要长得多find -type d。我怀疑这-empty是在检查所有文件以查看它们是否为空,然后-type d跳过这些文件。

还有:

1)优化查找的方法,以便a)找到所有文件夹,然后b)列出空文件夹?

或者

2)我可以使用哪些不同的命令来获取此列表?

答案1

尝试这个

find / -xdev -type d -exec find {}  -maxdepth 0 -empty  \;

或者稍微快一点

find / -xdev -type d | xargs -I{} find {} -maxdepth 0 -empty

答案2

find -type d | xargs -I{} find {} -empty

答案3

刚刚测试过,无法得到您的结果:

japhy@lizard:~ % time find . -type d |wc -l       
48403
find . -type d  0.87s user 8.69s system 4% cpu 3:37.60 total
japhy@lizard:~ % time find . -type d -empty |wc -l
3986
find . -type d -empty  0.79s user 4.41s system 57% cpu 9.071 total
japhy@lizard:~ % time find . -empty -type d |wc -l       
3986
find . -empty -type d  0.70s user 3.32s system 98% cpu 4.085 total
japhy@lizard:~ % find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX FTS() CBO(level=0) 

您的“查找”实用程序的版本是什么?分发版本?

答案4

find 的这些参数并不是按照 find 的顺序进行解释的开关——find 是一个命令行处理器,其中每个操作都是按顺序运行的测试。您的命令行按照您指定的顺序执行操作:查找目录,然后检查它们是否为空。我认为没有任何涉及 find 的技巧可以更快地完成此操作。

相关内容