March 13, 2018 · One-liner Server

Auto rm old backups one-liner

#!/bin/bash
date >> /home/backups/auto-backups/clear-backup.log
for d in /home/backups/auto-backups/*/ ; do
 find $d -type f -printf '%[email protected]\t%p\n' | sort -t $'\t' -g | head -n -3 | cut -d $'\t' -f 2- >> /home/backups/auto-backups/clear-backup.log
 find $d -type f -printf '%[email protected]\t%p\n' | sort -t $'\t' -g | head -n -3 | cut -d $'\t' -f 2- |xargs rm
done