带有更多小数位的“du -h”

Bra*_*one 6 linux bash format du

我正在开发一个用 bash 编写的小实用工具,它为我提供了有关 Linux 上游戏服务器的一些信息。因此,我需要能够显示文件内容的大小。

我现在正在这样做:

du -Lshc *
Run Code Online (Sandbox Code Playgroud)

它几乎完美!唯一的缺陷是它显示如下内容:

21G     backups
22G     server
8.0K    start.sh
151M    world.zip
43G     total
Run Code Online (Sandbox Code Playgroud)

这很好,但我想要更多的数字。这意味着它应该是这样的:

21.587G     backups
22.124G     server
8.089K      start.sh
151.198M    world.zip
43.436G     total
Run Code Online (Sandbox Code Playgroud)

有没有可能做到这一点?

我不介意使用复杂的命令,因为我在.sh文件中使用它,所以我不会每次都手动输入。

n.s*_*.st 7

du -Lsbc * | awk '
    function hr(bytes) {
        hum[1024**4]="TiB";
        hum[1024**3]="GiB";
        hum[1024**2]="MiB";
        hum[1024]="kiB";
        for (x = 1024**4; x >= 1024; x /= 1024) {
            if (bytes >= x) {
                return sprintf("%8.3f %s", bytes/x, hum[x]);
            }
        }
        return sprintf("%4d     B", bytes);
    }

    {
        print hr($1) "\t" $2
    }
'
Run Code Online (Sandbox Code Playgroud)

基于此的awk 函数。

通过管道输送column或用空格向左填充它可能会使输出看起来更好一些。

编辑:添加了左填充。

另外,对列表进行排序:du -Lsbc * | sort -n | awk然后是 awk-script。