Created
March 21, 2025 15:07
-
-
Save bendem/578b8fd9171c330b0390c248ec5d50e5 to your computer and use it in GitHub Desktop.
rotate files, keeping the last x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bad, if the recent backups failed, this can end up removing everything | |
find . -type f -name 'test-*' -mtime +5 -delete | |
# good, keeps 5 files at all time | |
keep=5 | |
find . -type f -name 'test-*' -printf '%T@\0%p\n' \ | |
| sort -rn \ | |
| cut -d $'\0' -f2 \ | |
| tail -n +$(( keep + 1 ) \ | |
| xargs rm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment