duを使用して、現在のディレクトリで最大20個のフォルダ/ファイルの最大サイズをバイトおよび人間が読める形式で再帰的に一覧表示します。

duを使用して、現在のディレクトリで最大20個のフォルダ/ファイルの最大サイズをバイトおよび人間が読める形式で再帰的に一覧表示します。

実装する必要がある事項 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

おすすめ記事