2

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
  • 193,181
  • 53
  • 473
  • 722
Gabriel Staples
  • 8,025
  • 7
  • 66
  • 105
  • I guess if `ncdu` has no option for it, it won't be possible. It can return `json` string with `ncdu -o- /boot` – pLumo Feb 07 '22 at 16:11
  • @pLumo, well, it's a computer. There's _always_ a way. It may just require a little bit of craziness. Ex: have the program screenshot the output, run OCR on it, move the mouse like a human, press keyboard keys, etc. Granted, that would be a very non-ideal solution. – Gabriel Staples Feb 07 '22 at 16:17
  • I posted a follow-up question at the end of the question. – Gabriel Staples Feb 07 '22 at 16:26
  • What does this question have to do with the unstated version of Ubuntu you use? – David Feb 07 '22 at 16:58
  • @David, probably nothing. Should I move the question to [Unix & Linux](https://unix.stackexchange.com/)? I'm on Ubuntu 20.04, by the way, but all that would change is the version of `ncdu`, in case it has come out with new options. – Gabriel Staples Feb 07 '22 at 16:59

1 Answers1

2

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

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

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

pLumo
  • 26,204
  • 2
  • 57
  • 87
  • 1
    Use `ncdu -f file.json` to read -o FILE Export scanned directory to FILE -f FILE Import scanned directory from FILE – Nickson Yap Dec 02 '22 at 03:08