find所有内部less于x个文件的目录

假设从文件系统的某个目录开始。 这个基目录有很多子目录(不嵌套!)。 每个子目录都有任意数量的文件。

我怎么能find所有的目录有例如less于3个文件使用一些shell命令? find命令有一些很好的选项来处理文件的大小,但是我找不到关于文件数量的任何信息。

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

这个怎么样:

 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) 

。 。 。 等等

我试图在-exec内部做到这一点,但是我感到不耐烦,只是将输出传递给输出并parsing出来。

编辑:注意到Quantas使用-1,并将其合并到我的脚本,因为你不能假设输出将在一个单一的列。 但是,我确实在脚本中留下了不足或相同的操作符。