Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a bunch of nested folders. Most folders contain files. Some contain hundreds of thousands of files. Some are empty.

I want to get a list of all empty folders. However, when I run:

find -type d -empty

it takes a very long time to run, a lot longer than it takes to run just find -type d. I suspect that -empty is checking all files to see if they are empty, then -type d is skipping the files.

So is there:

1) a way to optimize find so that it will a) find all folders, then b) list the empty ones?

or

2) a different command (or commands) that I could use to get this list?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Try this

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

or the marginally faster

find / -xdev -type d | xargs -I{} find {} -maxdepth 0 -empty
share|improve this answer
add comment
find -type d | xargs -I{} find {} -empty
share|improve this answer
    
This seems to have listed all the empty files in any of those folders. –  David Oneill Nov 3 '10 at 17:00
    
Hm. Good point. –  mfinni Nov 3 '10 at 17:44
add comment

Just tested, and I can't get your result:

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) 

What is your version of `find' utility? Distribution version?

share|improve this answer
    
find is version 4.4.0 –  David Oneill Nov 3 '10 at 17:38
add comment

Those arguments to find aren't switches to be interpreted in whatever order find feels like -- find is a command line processor, and each of those things is a test to run in sequence. Your command line is doing things in the exact order that you specify: it's finding directories, and then checking that they're empty. I don't think there are any tricks involving find to do this any faster.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.