Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
add comment

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!

share|improve this answer
    
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... –  Peter Cordes Dec 10 '09 at 3:09
    
Thanks Peter, I don't play with our AIX system outside of the basic usage often. :) –  Dan McGrath Dec 11 '09 at 2:26
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.