![du を使用して、現在のディレクトリ内の上位 20 個の最大のフォルダ/ファイルのサイズをバイト単位で再帰的に人間が読める形式で一覧表示します](https://rvso.com/image/154457/du%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E3%80%81%E7%8F%BE%E5%9C%A8%E3%81%AE%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88%E3%83%AA%E5%86%85%E3%81%AE%E4%B8%8A%E4%BD%8D%2020%20%E5%80%8B%E3%81%AE%E6%9C%80%E5%A4%A7%E3%81%AE%E3%83%95%E3%82%A9%E3%83%AB%E3%83%80%2F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E3%82%B5%E3%82%A4%E3%82%BA%E3%82%92%E3%83%90%E3%82%A4%E3%83%88%E5%8D%98%E4%BD%8D%E3%81%A7%E5%86%8D%E5%B8%B0%E7%9A%84%E3%81%AB%E4%BA%BA%E9%96%93%E3%81%8C%E8%AA%AD%E3%82%81%E3%82%8B%E5%BD%A2%E5%BC%8F%E3%81%A7%E4%B8%80%E8%A6%A7%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%BE%E3%81%99%20.png)
達成すべきことはいくつかあります。1. 上位20個の最大のフォルダ/ファイルを再帰的に取得する2. それらのサイズをバイト単位と人間が読める形式で取得する
答え1
#!/bin/bash
# ------------------------------------------
# Copy paste this content in a bash script e.g. ducks.sh
# And use it directly.
# ------------------------------------------
# Refer:
# https://www.cyberciti.biz/faq/linux-find-largest-file-in-directory-recursively-using-find-du/
# https://unix.stackexchange.com/a/220470/353485
function bytesToHR() {
local SIZE=$1
local UNITS="B KiB MiB GiB TiB PiB"
for F in $UNITS; do
local UNIT=$F
test ${SIZE%.*} -lt 1024 && break;
SIZE=$(echo "$SIZE / 1024" | bc -l)
done
if [ "$UNIT" == "B" ]; then
printf "%4.0f %s\n" $SIZE $UNIT
else
printf "%7.02f %s\n" $SIZE $UNIT
fi
}
du --block-size=1 --all ./ | sort -rn | head -n 20 > ./dump.txt
ALL_SIZES="`awk '{print $1}' ./dump.txt`"
# echo $ALL_SIZES
rm -f ./new_dump.txt
for s in $ALL_SIZES; do
bytesToHR $s >> ./new_dump.txt
done
paste ./new_dump.txt ./dump.txt