On 06/11/2010 03:47 PM, Peng Yu wrote:
> Hello,
> 
> I want to find all the directories that do not have any file in them
> (there can be directories in them). Could you show me how to do it?

Interesting challenge.  First, I created a test hierarchy:

$ ls -RF
.:
a/  b/  d/

./a:

./b:
c/  f

./b/c:

./d:
e/

./d/e:
g


Then, I used this.  It may not be the most efficient, but it works.  The
idea is to have find list directories, then to use -exec to have the
shell determine if the directory contains any non-directory children.
You are guaranteed that .* will resolve (each directory contains . and
..), but you have to special case * in case it didn't match.  The end
result is a bit long, but meets your requirements.

$ find -type d -exec sh -c 'cd -- "$1" && \
   for f in * .*; do test "$f" = "*" && test ! -e "$f" && continue; \
     test -d "$f" || exit 1; done' sh {} \; -print
.
./a
./b/c
./d

It correctly discarded ./b (because of ./b/f) and ./d/e (because of
./d/e/g) while still including ./b/c (a child of the discarded ./b) and
./d (a parent of the discarded ./d/e); if that's not exactly what you
wanted, then you need to tweak your question slightly.

-- 
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to