52 lines
1.2 KiB
Bash
52 lines
1.2 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -o errexit
|
||
|
|
set -o nounset
|
||
|
|
set -o pipefail
|
||
|
|
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
# shellcheck source=00_env.sh
|
||
|
|
. "$SCRIPT_DIR/00_env.sh"
|
||
|
|
|
||
|
|
SEARCH_PATH="/"
|
||
|
|
DEPTH=2
|
||
|
|
TOP_N=25
|
||
|
|
|
||
|
|
usage() {
|
||
|
|
printf 'Usage: %s [--path <path>] [--depth <N>] [--top <N>]\n' "$(basename "$0")"
|
||
|
|
}
|
||
|
|
|
||
|
|
while [[ "$#" -gt 0 ]]; do
|
||
|
|
case "$1" in
|
||
|
|
--path) SEARCH_PATH="${2:-}"; shift 2 ;;
|
||
|
|
--depth) DEPTH="${2:-}"; shift 2 ;;
|
||
|
|
--top) TOP_N="${2:-}"; shift 2 ;;
|
||
|
|
-h|--help) usage; exit 0 ;;
|
||
|
|
*) critical "Unknown argument: $1"; usage; exit 2 ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
|
||
|
|
if ! [[ "$DEPTH" =~ ^[0-9]+$ ]]; then
|
||
|
|
critical "--depth must be a non-negative integer"
|
||
|
|
exit 2
|
||
|
|
fi
|
||
|
|
|
||
|
|
if ! [[ "$TOP_N" =~ ^[0-9]+$ ]] || (( TOP_N < 1 )); then
|
||
|
|
critical "--top must be a positive integer"
|
||
|
|
exit 2
|
||
|
|
fi
|
||
|
|
|
||
|
|
validate_path "$SEARCH_PATH" || exit 2
|
||
|
|
require_cmd du || exit 1
|
||
|
|
require_cmd sort || exit 1
|
||
|
|
require_cmd head || exit 1
|
||
|
|
|
||
|
|
section "Top Directories Under $SEARCH_PATH"
|
||
|
|
warning "Read-only scan. Permission errors can be normal without root access."
|
||
|
|
|
||
|
|
du -x -h --max-depth="$DEPTH" "$SEARCH_PATH" 2>/dev/null |
|
||
|
|
sort -hr |
|
||
|
|
head -n "$TOP_N" |
|
||
|
|
tee -a "$LOG_FILE"
|
||
|
|
|
||
|
|
ok "No directories were modified."
|