diff --git a/System_Essentials/clear_logs.sh b/System_Essentials/clear_logs.sh index 9723ac7..16eeebf 100755 --- a/System_Essentials/clear_logs.sh +++ b/System_Essentials/clear_logs.sh @@ -143,8 +143,10 @@ if [[ "$SHOW_STATUS" == true ]]; then echo "━━━ System Logs ━━━" for f in "${LOG_FILES[@]}"; do if [[ -f "$f" ]]; then - size=$(du -sh "$f" 2>/dev/null | cut -f1) - size_mb=$(du -sm "$f" 2>/dev/null | cut -f1) + # One traversal, then formatted — this used to run du twice over the same path, + # once for the display string and once for the comparison. + size_mb=$(dir_size_mb "$f") || size_mb=0 + size=$(format_mb "$size_mb") threshold="${LOG_MIN_SIZE_MB:-10}" if [[ "${size_mb:-0}" -ge "$threshold" ]]; then echo " $ICON_WARN $f — $size (above ${threshold}MB threshold — would clear)" @@ -161,7 +163,7 @@ if [[ "$SHOW_STATUS" == true ]]; then if [[ -d /var/lib/docker/containers ]]; then find /var/lib/docker/containers/ -name "*-json.log" 2>/dev/null | \ while IFS= read -r logfile; do - size_mb=$(du -sm "$logfile" 2>/dev/null | cut -f1) + size_mb=$(dir_size_mb "$logfile") || size_mb=0 container_id=$(basename "$(dirname "$logfile")" | cut -c1-12) container_name=$(docker inspect --format '{{.Name}}' "$container_id" \ 2>/dev/null | tr -d '/' || echo "$container_id") @@ -204,9 +206,12 @@ for logfile in "${LOG_FILES[@]}"; do continue fi - size_bytes=$(stat -c%s "$logfile" 2>/dev/null || echo 0) - size_mb=$(( size_bytes / 1048576 )) - size_h=$(du -sh "$logfile" 2>/dev/null | cut -f1) + # Same basis as the dry-run preview above. This measured stat -c%s (apparent size) while the + # preview measured du (allocated blocks), so a file sitting on LOG_MIN_SIZE_MB could be shown + # as under the threshold and then cleared, or the reverse. A dry run that disagrees with the + # real run about what it will touch is worse than no dry run. + size_mb=$(dir_size_mb "$logfile") || size_mb=0 + size_h=$(format_mb "$size_mb") threshold="${LOG_MIN_SIZE_MB:-10}" if [[ "$size_mb" -lt "$threshold" ]]; then @@ -239,9 +244,10 @@ else while IFS= read -r logfile; do [[ -z "$logfile" ]] && continue - size_bytes=$(stat -c%s "$logfile" 2>/dev/null || echo 0) - size_mb=$(( size_bytes / 1048576 )) - size_h=$(du -sh "$logfile" 2>/dev/null | cut -f1) + # du basis, matching the "top 10 by size" listing above — that ranked and previewed on + # du while this cleared on stat, so the two could disagree about the same file. + size_mb=$(dir_size_mb "$logfile") || size_mb=0 + size_h=$(format_mb "$size_mb") threshold="${LOG_DOCKER_MAX_MB:-100}" # Get container name for display