Created
March 24, 2023 19:16
-
-
Save jhollinger/bde71769206eade97db50e26a223937d to your computer and use it in GitHub Desktop.
Keep most recent files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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