在 Linux 上,以下是ncdu
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
答案1
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
(磁盘大小)代替asize
(表观大小),或者向脚本引入参数以让用户决定。 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
答案2
这提供了可接受的最低答案,根据问题:
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
参考:
- 谢谢@terdon,他指出
sort -h
可以按照人类可读的大小进行排序,例如9.7 MiB
vs100 KiB
。
也可以看看:
- 问答@Fabien Auréjac 指出:服务器故障:如何按大小对 du -h 输出进行排序-
sort -h
可以对人类可读的输出进行排序du -h
答案3
du 确实提供了 -ah 选项,或者,您可以使用带有选项 -h 的 df 命令来获取可通过脚本解释的已用空间百分比数据。
要进行排序,您可以使用此 serverfault 帖子中显示的建议。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)