Home » Questions » Computers [ Ask a new question ]

AIX: find non-empty directories

AIX: find non-empty directories

In many unix systems, you could do something like

Asked by: Guest | Views: 35
Total answers/comments: 1
Guest [Entry]

"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."