How to recursively delete old files and prune resulting empty directories

aixfindshell-scripting

We're using an AIX server to house thousands and thousands of little files in a nested directory structure. I'm trying to write a script that will recursively delete old files, and then delete the containing directory if that was the last file in the directory.

For the purposes of examples, let's say any file older than 60 days is "old."

It sounds simple and easy, but I have looked around for a while and can't find a solution. Is there some combination of find and its flags, maybe pipelined with rmdir that will accomplish the above?

Best Answer

You've said you're on AIX. I think the basic shell commands will do the trick if I'm not wrong.

find /path/to/files* -mtime +60 -exec rm {} \;
find /path/to/files -type d -exec rmdir 2>/dev/null {} \;
Related Topic