AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 689668
Accepted
Gabriel Staples
Gabriel Staples
Asked: 2022-02-08 08:32:05 +0800 CST2022-02-08 08:32:05 +0800 CST 2022-02-08 08:32:05 +0800 CST

bash:使`du`显示类似于`ncdu`的输出

  • 772

ncdu在 Linux 上,以下是NCurses 磁盘使用工具的一些示例输出:

命令:

ncdu /boot
ncdu 1.14.1 ~ Use the arrow keys to navigate, press ? for help 
--- /boot -----------------------------------------------------
  100.2 MiB [##########]  initrd.img-5.13.0-28-generic         
  100.2 MiB [######### ]  initrd.img-5.13.0-27-generic
   11.2 MiB [#         ]  vmlinuz-5.11.0-46-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-28-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-27-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-25-generic
    8.0 MiB [          ] /grub
    5.7 MiB [          ]  System.map-5.13.0-28-generic
    5.7 MiB [          ]  System.map-5.13.0-27-generic
    5.7 MiB [          ]  System.map-5.13.0-25-generic
    5.6 MiB [          ]  System.map-5.11.0-46-generic
  252.0 KiB [          ]  config-5.13.0-28-generic
  252.0 KiB [          ]  config-5.13.0-27-generic
  252.0 KiB [          ]  config-5.13.0-25-generic
  252.0 KiB [          ]  config-5.11.0-46-generic
  184.0 KiB [          ]  memtest86+_multiboot.bin
  184.0 KiB [          ]  memtest86+.elf
  180.0 KiB [          ]  memtest86+.bin
!  16.0 KiB [          ] /lost+found
!   4.0 KiB [          ] /efi
@   0.0   B [          ]  initrd.img.old
@   0.0   B [          ]  initrd.img
@   0.0   B [          ]  vmlinuz.old
@   0.0   B [          ]  vmlinuz

但是,它是一个人机交互程序,并且输出不可编写脚本。我想将它存储到一个变量中,那么,我怎样才能获得类似的输出du呢?

这是我的问题的后续问题:如何ncdu快速显示磁盘使用情况并退出?

最终用途将如下所示:

output_before="$(du /boot)"
# do a bunch of stuff here which reduces the size of /boot
output_after="$(du /boot)"
echo "Before:"
echo "$output_before"
echo ""
echo "After:"
echo "$output_after"

这是一个开始,但它没有以正确的从大到小的降序显示输出:

du --all --max-depth=1 -h /boot

我最想看到的:

--- /boot -----------------------------------------------------
  100.2 MiB [##########]  initrd.img-5.13.0-28-generic         
  100.2 MiB [######### ]  initrd.img-5.13.0-27-generic
   11.2 MiB [#         ]  vmlinuz-5.11.0-46-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-28-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-27-generic
    9.7 MiB [          ]  vmlinuz-5.13.0-25-generic
    8.0 MiB [          ] /grub
    5.7 MiB [          ]  System.map-5.13.0-28-generic
    5.7 MiB [          ]  System.map-5.13.0-27-generic
    5.7 MiB [          ]  System.map-5.13.0-25-generic
    5.6 MiB [          ]  System.map-5.11.0-46-generic
  252.0 KiB [          ]  config-5.13.0-28-generic
  252.0 KiB [          ]  config-5.13.0-27-generic
  252.0 KiB [          ]  config-5.13.0-25-generic
  252.0 KiB [          ]  config-5.11.0-46-generic
  184.0 KiB [          ]  memtest86+_multiboot.bin
  184.0 KiB [          ]  memtest86+.elf
  180.0 KiB [          ]  memtest86+.bin
   16.0 KiB [          ] /lost+found
    4.0 KiB [          ] /efi
    0.0   B [          ]  initrd.img.old
    0.0   B [          ]  initrd.img
    0.0   B [          ]  vmlinuz.old
    0.0   B [          ]  vmlinuz

但是,可接受的最低答案如下所示:

  100.2 MiB  /boot/initrd.img-5.13.0-28-generic         
  100.2 MiB  /boot/initrd.img-5.13.0-27-generic
   11.2 MiB  /boot/vmlinuz-5.11.0-46-generic
    9.7 MiB  /boot/vmlinuz-5.13.0-28-generic
    9.7 MiB  /boot/vmlinuz-5.13.0-27-generic
    9.7 MiB  /boot/vmlinuz-5.13.0-25-generic
    8.0 MiB  /boot/grub
    5.7 MiB  /boot/System.map-5.13.0-28-generic
    5.7 MiB  /boot/System.map-5.13.0-27-generic
    5.7 MiB  /boot/System.map-5.13.0-25-generic
    5.6 MiB  /boot/System.map-5.11.0-46-generic
  252.0 KiB  /boot/config-5.13.0-28-generic
  252.0 KiB  /boot/config-5.13.0-27-generic
  252.0 KiB  /boot/config-5.13.0-25-generic
  252.0 KiB  /boot/config-5.11.0-46-generic
  184.0 KiB  /boot/memtest86+_multiboot.bin
  184.0 KiB  /boot/memtest86+.elf
  180.0 KiB  /boot/memtest86+.bin
   16.0 KiB  /boot/lost+found
    4.0 KiB  /boot/efi
    0.0   B  /boot/initrd.img.old
    0.0   B  /boot/initrd.img
    0.0   B  /boot/vmlinuz.old
    0.0   B  /boot/vmlinuz
disk-usage ncdu
  • 3 3 个回答
  • 776 Views

3 个回答

  • Voted
  1. Gabriel Staples
    2022-02-08T08:58:43+08:002022-02-08T08:58:43+08:00

    根据以下问题,这提供了可接受的最低答案:

    du --all --max-depth=1 -h /boot | sort -rh
    

    示例用法,根据问题中的示例(但制成“单行”命令,因此我可以将其复制粘贴到终端中,而无需为其制作可执行文件脚本):

    output="$(du --all --max-depth=1 -h /boot | sort -rh)"; \
    echo "Before:"; \
    echo "$output"
    

    输出到标准输出的示例:

    Before:
    273M    /boot
    101M    /boot/initrd.img-5.13.0-28-generic
    101M    /boot/initrd.img-5.13.0-27-generic
    12M /boot/vmlinuz-5.11.0-46-generic
    9.8M    /boot/vmlinuz-5.13.0-28-generic
    9.7M    /boot/vmlinuz-5.13.0-27-generic
    9.7M    /boot/vmlinuz-5.13.0-25-generic
    8.0M    /boot/grub
    5.7M    /boot/System.map-5.13.0-28-generic
    5.7M    /boot/System.map-5.13.0-27-generic
    5.7M    /boot/System.map-5.13.0-25-generic
    5.6M    /boot/System.map-5.11.0-46-generic
    252K    /boot/config-5.13.0-28-generic
    252K    /boot/config-5.13.0-27-generic
    252K    /boot/config-5.13.0-25-generic
    252K    /boot/config-5.11.0-46-generic
    184K    /boot/memtest86+_multiboot.bin
    184K    /boot/memtest86+.elf
    180K    /boot/memtest86+.bin
    16K /boot/lost+found
    4.0K    /boot/efi
    0   /boot/vmlinuz.old
    0   /boot/vmlinuz
    0   /boot/initrd.img.old
    0   /boot/initrd.img
    

    参考:

    1. 感谢@terdon,他指出 sort -h可以按人类可读的大小进行排序,例如9.7 MiBvs 100 KiB。

    也可以看看:

    1. @Fabien Auréjac 指出的问答:服务器故障:如何按大小对 du -h 输出进行排序-sort -h可以对人类可读的输出进行排序du -h
    • 6
  2. Best Answer
    pLumo
    2022-02-08T09:05:25+08:002022-02-08T09:05:25+08:00

    python从以下内容读取的小脚本ncdu -o-:

    read_ncdu.py:

    #!/usr/bin/env python3
    
    import sys, json
    
    def sizeof_fmt(num, suffix='B'):
        for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
            if abs(num) < 1024.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1024.0
        return "%.1f%s%s" % (num, 'Yi', suffix)
    
    def get_recursive(item):
        size = 0
        if isinstance(item, dict):
            name = item["name"]
            size = item["asize"]
        else:
            name = item[0]["name"]
            for sub in item:
                size += get_recursive(sub)[1]
        return (name, size)
    
    data = json.loads(sys.stdin.read())
    
    items=[]
    for i in data[3][1:]:
        items.append(get_recursive(i))
    
    sum_sizes = sum([item[1] for item in items])
    biggest = max([item[1] for item in items])
    print("------ {} --- {} -------".format(data[3][0]["name"], sizeof_fmt(sum_sizes)))
    
    for item in sorted(items, key=lambda x:x[1], reverse=True):
        size=item[1]
        hsize=sizeof_fmt(item[1])
        name=item[0]
        percent=(size/sum_sizes*100)
        percent_str="({:.1f}%)".format(percent)
        
        print("{} {:8} [{}{}] {}".format(
            " " * (10 - len(str(hsize)))+ str(hsize),
            " " * (8 - len(percent_str)) + percent_str,  
            ('#' * round(size/biggest*10)),
            ('-' * round(10-size/biggest*10)),
            item[0])
        )
    

    您可能想要改进脚本:

    • 如果您愿意,可以使用dsize(disk size) 代替asize(apparant size),或者在脚本中引入参数让用户决定。
    • os.walk()使用而不是使用ncdu -o-输入使脚本独立。

    有关json 输出格式的说明,另请参见此处。ncdu

    跑:

    使read_ncdu.py可执行文件 --> chmod +x read_ncdu.py,然后你可以运行:

    ncdu -o- /boot | ./read_ncdu.py
    

    输出:

    ------ /boot --- 224.3MiB -------
       56.8MiB  (25.3%)  [##########]  initrd.img-5.13.0-28-generic
       56.7MiB  (25.3%)  [##########]  initrd.img-5.13.0-27-generic
       55.4MiB  (24.7%)  [##########]  initrd.img-5.11.0-46-generic
       11.2MiB  (5.0%)   [##--------]  vmlinuz-5.11.0-46-generic
        9.7MiB  (4.3%)   [##--------]  vmlinuz-5.13.0-28-generic
        9.7MiB  (4.3%)   [##--------]  vmlinuz-5.13.0-27-generic
        6.5MiB  (2.9%)   [#---------]  grub
        5.7MiB  (2.5%)   [#---------]  System.map-5.13.0-28-generic
        5.7MiB  (2.5%)   [#---------]  System.map-5.13.0-27-generic
        5.6MiB  (2.5%)   [#---------]  System.map-5.11.0-46-generic
      251.7KiB  (0.1%)   [----------]  config-5.13.0-28-generic
      251.6KiB  (0.1%)   [----------]  config-5.13.0-27-generic
      248.1KiB  (0.1%)   [----------]  config-5.11.0-46-generic
      180.6KiB  (0.1%)   [----------]  memtest86+_multiboot.bin
      180.1KiB  (0.1%)   [----------]  memtest86+.elf
      178.4KiB  (0.1%)   [----------]  memtest86+.bin
       16.0KiB  (0.0%)   [----------]  lost+found
         28.0B  (0.0%)   [----------]  initrd.img
         28.0B  (0.0%)   [----------]  initrd.img.old
         25.0B  (0.0%)   [----------]  vmlinuz
         25.0B  (0.0%)   [----------]  vmlinuz.old
    
    • 6
  3. Fabien Auréjac
    2022-02-08T08:35:49+08:002022-02-08T08:35:49+08:00

    du 确实提供了 -ah 选项,或者,您可以使用带有选项 -h 的 df 命令来获取可以通过脚本解释的已用空间百分比数据。

    要进行排序,您可以使用此 serverurfault 帖子上显示的建议。https://serverfault.com/questions/62411/how-can-i-sort-du-h-output-by-size

    du -hs * | sort -h
    

    来自 GNUsort 手册:

    -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G)

    • 1

相关问题

  • 如何使用df计算总磁盘空间?

  • 使用 DD 在外部读取器中写入原始磁盘时出现磁盘空间错误?

  • 如何提高磁盘空间使用率?

  • 具有更细粒度的人类可读的“du”

  • 除了完全使用的文件夹之外,“设备上没有剩余空间”的其他原因是什么?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve