Docker Cleanup Preview
Show reclaimable Docker space, then prune only with an explicit flag.
$ webtoolist --script safe-log-cleanup
Delete rotated log files older than a set number of days, with a dry-run first.
logscleanupdiskmaintenanceDependencies: find, date
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "usage: $0 [--days N] [--delete] [LOG_DIR]" >&2
exit 2
}
days=14
delete=0
log_dir=""
while (($#)); do
case "$1" in
--days) [[ ${2:-} =~ ^[0-9]+$ ]] || usage; days="$2"; shift 2 ;;
--delete) delete=1; shift ;;
-h|--help) usage ;;
-*) usage ;;
*) log_dir="$1"; shift ;;
esac
done
log_dir="${log_dir:-/var/log}"
[[ -d "$log_dir" ]] || { echo "not a directory: $log_dir" >&2; exit 1; }
mapfile -t victims < <(
find "$log_dir" -maxdepth 1 -type f \
\( -name '*.log.*' -o -name '*.log-*' -o -name '*.gz' \) \
-mtime +"$days" -print | sort
)
if ((${#victims[@]} == 0)); then
echo "no log files older than ${days} days in $log_dir"
exit 0
fi
printf '%s\n' "${victims[@]}"
if ((delete)); then
printf 'deleting %d file(s) older than %d days...\n' "${#victims[@]}" "$days"
printf '%s\0' "${victims[@]}" | xargs -0 rm -f --
echo "done"
else
echo "(dry run) re-run with --delete to remove these files"
fi
sudo ./safe-log-cleanup --days 30 /var/log
sudo ./safe-log-cleanup --days 30 --delete /var/log
/etc/logrotate.d first.Related scripts
Show reclaimable Docker space, then prune only with an explicit flag.
Locate files above a size threshold and show them largest first.
Show filesystem usage and the largest directories under a path.
List systemd units that are failed or restarting, with recent log lines.