Last active
August 18, 2018 13:11
-
-
Save tueda/caeb67bd8d5e3b0697f8fd6e8b8a79ae to your computer and use it in GitHub Desktop.
Who uses so much disk space or number of inodes?
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 | |
set -eu | |
set -o pipefail | |
# NOTE: numfmt and du --inode require GNU coreutils >=8.21 (Feb 2013), | |
# So they are not available in CentOS 6. | |
progname=$(basename "$0") | |
print_help() { | |
cat <<END | |
Usage: $progname [options] | |
Options: | |
-i report inode usage | |
-h print numbers in human readable format | |
END | |
} | |
opt_inodes=false | |
opt_human_redable=false | |
for a; do | |
case $a in | |
-i) | |
opt_inodes=: | |
;; | |
-h) | |
opt_human_redable=: | |
;; | |
-ih|-hi) | |
opt_inodes=: | |
opt_human_redable=: | |
;; | |
--help) | |
print_help | |
exit | |
;; | |
*) | |
echo "$progname: error: unknown option $a" 2>&1 | |
exit 1 | |
;; | |
esac | |
done | |
# https://unix.stackexchange.com/a/44087 | |
my_numfmt() { | |
awk ' | |
function human(x) { | |
if (x<1000) {return x} else {x/=1024} | |
s="KMGTEPZY"; | |
while (x>=1000 && length(s)>1) | |
{x/=1024; s=substr(s,2)} | |
return int(x+0.5) substr(s,1,1) | |
} | |
{sub(/^[0-9]+/, human($1)); print}' | |
} | |
{ | |
if $opt_inodes; then | |
if $opt_human_redable; then | |
output_filter=my_numfmt | |
else | |
output_filter=cat | |
fi | |
{ | |
for d in $(find . -maxdepth 1 -type d); do | |
case $d in | |
.|..) | |
;; | |
*) | |
echo "$(find $d -type f | wc -l) $d" | |
esac | |
done | |
echo "$(find . -maxdepth 1 -type f | wc -l) ." | |
} | $output_filter | column -t | |
else | |
if $opt_human_redable; then | |
du --max-depth=1 -h | |
else | |
du --max-depth=1 | |
fi | |
fi | |
} | sort -h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment