Skip to content

Instantly share code, notes, and snippets.

@MatthewJohn
Last active February 21, 2019 12:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewJohn/afc143dd40fe09a34d0e3d5f9584957b to your computer and use it in GitHub Desktop.
Save MatthewJohn/afc143dd40fe09a34d0e3d5f9584957b to your computer and use it in GitHub Desktop.
One-liner prune oldest file to reach disk percentage free
#!/bin/bash
export WANTED_PERC_FREE=92 DEL_DIR=./ MAX_DELETIONS=10; for i in $(find ${DEL_DIR} -type f -printf "%TY%Tj %p\n" | sort -n | gawk '{ print $2 }' | head --lines=$MAX_DELETIONS); do if (( $(df ${DEL_DIR} --output=pcent | tail -1 | sed 's/[^0-9]//g') > $WANTED_PERC_FREE )); then rm $i; else exit 1; fi; done
@MatthewJohn
Copy link
Author

One liner to delete all files in directory, from oldest to newest, in order to reach a given disk utilisation percentage, with a hard limit of deletions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment