Skip to content

Instantly share code, notes, and snippets.

@0xBADC0FFEE
Created May 18, 2018 12:09
Show Gist options
  • Save 0xBADC0FFEE/800304dae1eb8c3cc27c00e9e907a662 to your computer and use it in GitHub Desktop.
Save 0xBADC0FFEE/800304dae1eb8c3cc27c00e9e907a662 to your computer and use it in GitHub Desktop.
Bash script that remove oldest files recursively from a directory if free size is over a threshold
#!/bin/bash
DIRECTORY="/path/to/your/directory"
CAPACITY=95
while [[ $(df $DIRECTORY | awk 'NR==2 && gsub("%","") {print$5}') -ge $CAPACITY ]];do
rm -rf $(find $DIRECTORY -mindepth 1 -printf '%T+ %p\n' | sort | awk 'NR==1 {print$2}')
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment