Last active
February 2, 2022 06:22
-
-
Save vazhnov/177fa8dfb474ed108860731dca21c591 to your computer and use it in GitHub Desktop.
Remove oldest .tgz files, if free space less than 50GB
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
#!/usr/bin/env bash | |
# set -o nounset | |
set -o errexit | |
shopt -s dotglob | |
# Remove oldest .tgz files, if free space less than 50GB | |
# | |
# License: CC0 1.0 or newer | |
# https://creativecommons.org/publicdomain/zero/1.0/ | |
# | |
# You can download this script here: https://gist.github.com/vazhnov/177fa8dfb474ed108860731dca21c591 | |
dir="/tmp" | |
while [ $(df --output=avail ${dir} | tail -n 1) -lt 50000000 ]; do | |
# See for details: http://mywiki.wooledge.org/BashFAQ/003 | |
unset -v oldest | |
for file in "$dir"/*tgz; do | |
[[ -z $oldest || $file -ot $oldest ]] && oldest=$file | |
done | |
rm -f "${oldest}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another logic: delete oldest file if there are more than 6 files in the current working directory: https://gist.github.com/maximiliankolb/ab6670d3bd86fa91af7aac42c646cd77