In many unix systems, you could do something like

find . -type d ! -empty

In an AIX system, -empty doesn't seem to be recognized. Any ideas on how I could do the same thing?

2 Answers 2


Dan McG has the right idea. This works:

find  . -type d -links 2 -exec sh -c '[ -z "$(ls -UA "$1/" )" ]' dummy {} \; -print

-links 2 saves time by excluding directories with subdirs, because if AIX is like most Unix systems, the only links a directory can have are its own . entry, and the .. entries in its subdirs.

ls -UA doesn't bother to sort (-U), and doesn't need to stat the files, just output what it gets from readdir, so it's not too bad. -A omits . and .., so if the output is the empty string, the directory was empty.

If you're invoking sh -c anyway, you could do what you need per-empty-directory right in the sh, instead of using the output of find -print0. Too bad non-GNU find doesn't have -exec {} + (i.e. xargs built in).

Maybe if you process the output of find, you can do it more efficiently. e.g. if you see two directories in a row without a file, then you have an empty directory? it's not that slow to run sh -c and ls for every directory, though.


Firstly, empty isn't specified for find in the POSIX standard, which is why not all versions support it.

I'm not sure exactly how to do it, but this might point you in a direction to solve it.

find . -type d -exec command '{}' \;

This will execute 'command' for each directory, which the directory it is up to as the argument.

Now, you can use something like:

find . -type d -exec sh -c 'ls -R $1 | wc' dummy {} \;

Obviously, this doesn't work, but it might help a bit!

  • That should be sh -c '...' dummy {} \; The first arg to sh -c becomes $0. That bit me the first time I tried find -exec sh... Dec 10, 2009 at 3:09
  • Thanks Peter, I don't play with our AIX system outside of the basic usage often. :) Dec 11, 2009 at 2:26

You must log in to answer this question.

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