Skip to content

Instantly share code, notes, and snippets.

@meyju
Last active July 14, 2016 08:46
Show Gist options
  • Save meyju/e677070274c968ba8823faa0eb6dd807 to your computer and use it in GitHub Desktop.
Save meyju/e677070274c968ba8823faa0eb6dd807 to your computer and use it in GitHub Desktop.
Remove all files older than X days, but keep at least the Y youngest
KEEP=2 OLDERTHEN=4;find /backups/ -type f -mtime +${OLDER} -printf '%T@ %p\n'|sort -nr | tail -n +${KEEP}|awk '{print $2}'|xargs -r rm -f
# For Crontab: escape the % in printf option => -printf '\%T@ \%p\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment