Skip to content

Instantly share code, notes, and snippets.

@vazhnov
Last active February 2, 2022 06:22
Show Gist options
  • Save vazhnov/177fa8dfb474ed108860731dca21c591 to your computer and use it in GitHub Desktop.
Save vazhnov/177fa8dfb474ed108860731dca21c591 to your computer and use it in GitHub Desktop.
Remove oldest .tgz files, if free space less than 50GB
#!/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
@vazhnov
Copy link
Author

vazhnov commented Aug 2, 2018

Another logic: delete oldest file if there are more than 6 files in the current working directory: https://gist.github.com/maximiliankolb/ab6670d3bd86fa91af7aac42c646cd77

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