Skip to content

Instantly share code, notes, and snippets.

@jhollinger
Created March 24, 2023 19:16
Show Gist options
  • Save jhollinger/bde71769206eade97db50e26a223937d to your computer and use it in GitHub Desktop.
Save jhollinger/bde71769206eade97db50e26a223937d to your computer and use it in GitHub Desktop.
Keep most recent files
# Delete all but the most recent N files in the given dir (and its subdirs)
function keep_most_recent {
dir="$1"
n=$2
offset=$((n+1))
find "$dir" -type f -printf "%T+ %p\n" |
sort -r |
awk '{ print $2 }' |
tail -n +$offset |
xargs --no-run-if-empty rm -f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment