2

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?

4 Answers 4

2

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
1
find -type d | xargs -I{} find {} -empty
2
  • This seems to have listed all the empty files in any of those folders. Commented Nov 3, 2010 at 17:00
  • Hm. Good point.
    – mfinni
    Commented Nov 3, 2010 at 17:44
0

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?

1
  • find is version 4.4.0 Commented Nov 3, 2010 at 17:38
0

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .