Skip to content

Instantly share code, notes, and snippets.

@coderoffortune
Last active April 19, 2017 14:35
Show Gist options
  • Save coderoffortune/c3622c4019198cab98a9 to your computer and use it in GitHub Desktop.
Save coderoffortune/c3622c4019198cab98a9 to your computer and use it in GitHub Desktop.
Quickly remove old backup files from backup folder to avoid clutter and keep disk space usage at bay
#Explanation:
# ls -t => Lists the files in a folder in decrescent order of modification time, so it will list newer files first
# sed -e '1,50d' => Filters the first Nth (50 in this case) lines leaving only the oldest files
# grep -v "lost+found" => Optional, if you are in a backup disk root you will need this probably, otherwise you can omit it
# awk '{print "/mnt/backup/"$1}' => Prepend the absolute path to the file name so it's easier to run it in a cron job
# xargs -d '\n' rm => Takes every lines from the pipe, strips out the newline char and passes it to the rm command
ls -t /mnt/backup | sed -e '1,50d' | grep -v "lost+found" | awk '{print "/mnt/backup/"$1}' | xargs -d '\n' rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment