I have the following task: delete old "builds" older than 30 days. And this solution works perfectly:
find $jenkins_jobs -type d -name builds -exec find {} -type d -mtime +30 \; >> $filesToBeDelete
cat $filesToBeDelete | xargs rm -rf
But later some condition were added: delete only in case when we have more than 30 builds and clean the oldest ones. So in results we should keep 30 newest build and delete rest.
Also I have found that I can use if statement in find like that:
if [ $(find bla-bla | wc -l) -gt 30 ]; then
...
fi
but I am wandering how can I delete that files.
Is it clear? For example we have in "build" folder 100 builds and all of them are older than 30 days. So I want to keep 30 new builds and delete another 70.