Report the Largest Files Under a Tree
Scan a directory tree, sort files by size, and print the largest offenders so disk pressure can be investigated quickly.
Code
#!/usr/bin/env bash
set -euo pipefail
readonly ROOT="${1:-.}"
readonly TOP_N="${2:-10}"
if [[ ! -d "${ROOT}" ]]; then
echo "not a directory: ${ROOT}" >&2
exit 2
fi
find "${ROOT}" -type f -size +10M -print0 \
| xargs -0 du -h 2>/dev/null \
| sort -rh \
| head -n "${TOP_N}"Report a problem with this exampleOpens a pre-filled issue on GitHub.