Created
June 15, 2019 11:40
-
-
Save attie/e7bb4166a2b94d0153199e04d767dd54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eu | |
if [ $# -gt 0 ]; then | |
BASE_DIR="$(readlink -e "${1}")"; shift | |
else | |
BASE_DIR="$(readlink -e .)"; | |
fi | |
if [ $# -gt 0 ]; then | |
( | |
echo "usage:" | |
echo " $0 [directory]" | |
) >&2 | |
exit 1 | |
fi | |
is_in() { | |
[ $# -lt 1 ] && return 1 | |
seek="${1}"; shift | |
for v in "${@}"; do | |
[ "${v}" == "${seek}" ] && return 0 | |
done | |
return 1 | |
} | |
declare -A SPECIAL | |
SPECIAL[b]='block' | |
SPECIAL[c]='char' | |
SPECIAL[l]='symlink' | |
SPECIAL[p]='pipe' | |
SPECIAL[s]='socket' | |
find "${BASE_DIR}" -mindepth 1 -maxdepth 1 \ | |
| while read ent; do | |
if mountpoint -q -- "${ent}"; then | |
printf "%-8s${ent}\n" "mount" >&2 | |
else | |
TYPE="$(stat -c '%A' "${ent}" | cut -b1)" | |
if is_in "${TYPE}" "${!SPECIAL[@]}"; then | |
printf "%-8s${ent}\n" "${SPECIAL[${TYPE}]}" >&2 | |
elif [ "${TYPE}" == "d" ]; then | |
echo "${ent}/" | |
elif [ "${TYPE}" == "-" ]; then | |
echo "${ent}" | |
else | |
printf "%-8s${ent}\n" "unknown" >&2 | |
fi | |
fi | |
done \ | |
| xargs -rd '\n' du -bhsx -- \ | |
| sort -h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment