4

I'd like to make the ncdu NCurses Disk Usage tool analyze a path, display the output, and exit, for scripting purposes. Normally it is a human-interactive tool, but I'd like to remove the human-interactive part of it.

If I run ncdu /boot I can see my /boot partition. Here is what I see:

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

Now, how can I script this to display this output, store it into a variable for later printing, and exit?

What I'd like is something like this:

output="$(ncdu /boot)"
echo "$output"

Currently it hangs at the first line since it's waiting on human interaction I think.

Follow-on question: bash: make du show output similar to ncdu

muru
  • 207,228
Gabriel Staples
  • 11,502
  • 14
  • 97
  • 142

2 Answers2

7

With ncdu, it is not possible. It uses ncurses library to display the results with interactive features and (unlike e.g. top with -b Batch-mode option) it has no direct option to print what it displays.

However, it can print or save to a json file, with all information you need, that can be parsed: ncdu -o file.json or to stdout: ncdu -o-.

Use ncdu -f file.json to read the exported file.

Also, I provided a python script to parse this to a similar output over at Unix SE.

Pablo Bianchi
  • 17,371
pLumo
  • 27,991
2

The ncdu webpage lists similar projects at the bottom, one of being tdu "Top Disk Usage" that does exit non-interactively with a text summary (including biggest nested files).

Another one is gdu. Both have ncurses-interface and non-interactive exit built-in.

Pablo Bianchi
  • 17,371
wbob
  • 549