-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_usage_hotspots.sh
More file actions
executable file
·49 lines (40 loc) · 1.29 KB
/
disk_usage_hotspots.sh
File metadata and controls
executable file
·49 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
set -euo pipefail
# disk_usage_hotspots.sh
# Lists largest directories/files under a path to identify disk hotspots.
usage() {
cat <<'EOF'
Usage: ./disk_usage_hotspots.sh [options]
Options:
--path DIR Root path to scan (default: /)
--depth N Directory depth for du scan (default: 2)
--top N Number of entries to show (default: 30)
--files Include largest files listing
-h, --help Show help
EOF
}
SCAN_PATH="/"
DEPTH=2
TOP_N=30
SHOW_FILES=false
while [[ $# -gt 0 ]]; do
case "$1" in
--path) SCAN_PATH="$2"; shift 2 ;;
--depth) DEPTH="$2"; shift 2 ;;
--top) TOP_N="$2"; shift 2 ;;
--files) SHOW_FILES=true; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
done
if [[ ! -d "$SCAN_PATH" ]]; then
echo "Path not found: $SCAN_PATH"
exit 1
fi
echo "Top $TOP_N directories under $SCAN_PATH (depth=$DEPTH):"
du -x -h --max-depth="$DEPTH" "$SCAN_PATH" 2>/dev/null | sort -hr | head -n "$TOP_N"
if [[ "$SHOW_FILES" == true ]]; then
echo
echo "Top $TOP_N files under $SCAN_PATH:"
find "$SCAN_PATH" -xdev -type f -printf "%s %p\n" 2>/dev/null | sort -nr | head -n "$TOP_N" | awk '{printf "%.2f MB %s\n", $1/1024/1024, substr($0, index($0,$2))}'
fi