Skip to content

Instantly share code, notes, and snippets.

@jlinoff
Created December 6, 2017 13:23
Show Gist options
  • Save jlinoff/ca0e5a2187e86abdf4b7802707df2250 to your computer and use it in GitHub Desktop.
Save jlinoff/ca0e5a2187e86abdf4b7802707df2250 to your computer and use it in GitHub Desktop.
Bash script to purge all but the last N files that match a pattern.
#/bin/bash
#
# Purge all but the last N files in the input list.
#
if (( $# > 1 )) ; then
readonly NUM=$1
shift
readonly LIST=($*)
readonly LEN=$(( ${#LIST[@]} - $NUM ))
if (( LEN )) ; then
echo "purging ${LIST[@]:0:$LEN}"
rm -rf "${LIST[@]:0:$LEN}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment