bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to find all the directories that don't have any file in it?


From: Eric Blake
Subject: Re: How to find all the directories that don't have any file in it?
Date: Mon, 14 Jun 2010 08:50:34 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100430 Fedora/3.0.4-3.fc13 Lightning/1.0b2pre Mnenhy/0.8.2 Thunderbird/3.0.4

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   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]