In Linux, how can I delete directories whose contents have not been accessed in several days?

28 views Asked by At

I need to clean up some cache directories on build servers.

I only want to delete a directory if NONE of its descendent files have been read within 14 days.

I cooked the following up, and while it's only a bit slower (3m) than a plain find (1m45), I'm thinking there should be a more elegant solution.

caches=$(find $HOME/.gradle/caches -maxdepth 2 -type d \( -name scripts -or -name transforms-\* \) )
find $caches -maxdepth 1 -mindepth 1 -type d |
  while read ; do
    young=$( find "$REPLY" -type f -atime -14 -print -quit )
    [[ -n "$young" ]] || rm -rf "$REPLY"
  done

Any suggestions?

(Aside: I had a look for Gradle-specific cache cleaning tasks, but only found cleanBuildCache which was Android-only and is deprecated now anyhow. My colleague has since found this)

Edit: Added -print -quit. Also fixed stupid 'find ||' mistake.

0

There are 0 answers