Skip to content

Instantly share code, notes, and snippets.

@DartPower
Forked from vazhnov/remove_oldest_files.sh
Created February 2, 2022 06:22
Show Gist options
  • Save DartPower/1ba0d17c87bc7fdd069702a15c48f7dc to your computer and use it in GitHub Desktop.
Save DartPower/1ba0d17c87bc7fdd069702a15c48f7dc 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment