Score:1

How to make `ncdu` show a quick summary of disk usage and exit?

cn flag

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

pLumo avatar
in flag
I guess if `ncdu` has no option for it, it won't be possible. It can return `json` string with `ncdu -o- /boot`
Gabriel Staples avatar
cn flag
@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 avatar
cn flag
I posted a follow-up question at the end of the question.
David avatar
cn flag
What does this question have to do with the unstated version of Ubuntu you use?
Gabriel Staples avatar
cn flag
@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.
Score:2
in flag

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.

Nickson Yap avatar
sd flag
Use `ncdu -f file.json` to read -o FILE Export scanned directory to FILE -f FILE Import scanned directory from FILE
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.