查找所有包含少于 x 个文件的目录

查找所有包含少于 x 个文件的目录

假设从文件系统中的某个目录开始。此基本目录有许多子目录(非嵌套!)。每个子目录中都有任意数量的文件。

如何使用某些 shell 命令查找所有包含少于 3 个文件的目录? find 命令有一些不错的选项可用于处理文件大小,但我找不到任何有关文件计数的信息。

答案1

$ find . -type d | while read d; do if [ $(ls -1 "$d" | wc -l) -lt 3 ]; then echo $d; fi; done

答案2

这个怎么样:

for i in `find /etc/ -type d `; do j=`ls -1 $i | wc -l` ; if [ $j -le  3 ]; then echo "$i has about $j file(s)"; fi; done

输出:

[root@kerberos ~]# for i in `find /etc/ -type d `; do j=`ls $i | wc -l` ; if [ $j -le  3 ]; then echo "$i has about $j file(s)"; fi; done
/etc/prelink.conf.d has about 0 file(s)
/etc/kdump-adv-conf has about 2 file(s)
/etc/kdump-adv-conf/kdump_initscripts has about 2 file(s)
/etc/kdump-adv-conf/kdump_sample_manifests has about 1 file(s)
/etc/openldap/slapd.d.backup has about 2 file(s)
/etc/openldap/ssl has about 2 file(s)
/etc/foomatic has about 2 file(s)
/etc/gtk-2.0 has about 2 file(s)
/etc/gtk-2.0/x86_64-redhat-linux-gnu has about 2 file(s)

。 。 。 ETC

我尝试在 -exec 中执行此操作,但是我没有耐心,只是通过管道传输输出并将其解析出来。

编辑:注意到 Quantas 使用 -1 并将其合并到我的脚本中,因为您不能假设输出将在单个列中。但是,我确实在脚本中保留了小于或等于运算符。

相关内容