DEV Community

Cover image for Check Disk Usage with Terminal
Vladimir Vovk
Vladimir Vovk

Posted on

Check Disk Usage with Terminal

At some point, we all face the problem of free space on our hard drives. I usually check how much space a particular folder takes with the du terminal command. It shows the size of the folder itself and its subfolders.

$ du node_modules
0       node_modules/.bin
72      node_modules/commander/typings
240     node_modules/commander/lib
456     node_modules/commander
8       node_modules/prettier/bin
12416   node_modules/prettier/plugins
248     node_modules/prettier/internal
15304   node_modules/prettier
32      node_modules/chalk/source/vendor/supports-color
32      node_modules/chalk/source/vendor/ansi-styles
64      node_modules/chalk/source/vendor
104     node_modules/chalk/source
144     node_modules/chalk
15928   node_modules
Enter fullscreen mode Exit fullscreen mode

To make it prettier we need to pass a couple of parameters. -h for "human readable" output and -d controls depth directories deep.

$ du -h -d 1 node_modules
  0B    node_modules/.bin
228K    node_modules/commander
7.5M    node_modules/prettier
 72K    node_modules/chalk
7.8M    node_modules
Enter fullscreen mode Exit fullscreen mode

Now we can sort the output with the sort command. Where -h is for "human numeric" sort and -r for reverse.

$ du -h -d 1 node_modules | sort -hr
7.8M    node_modules
7.5M    node_modules/prettier
228K    node_modules/commander
 72K    node_modules/chalk
  0B    node_modules/.bin
Enter fullscreen mode Exit fullscreen mode

Looks good, right? But could we have a simpler command with better defaults?

Yes! Meet hdu the human-friendly du wrapper.

Please make sure you have Node.js installed on your machine.

Type npx hdu node_modules to get information about the node_modules folder.

npx hdu output

By default, it will display the sizes of all directories from the selected folder (or current folder if not specified) and sort them descending by their size.

Did you notice that the .bin directory is not in the list? That's because its size is equal to zero. 🥲

We can also display files with the --include-files option.

npx hdu -f output

There is a --sort option to change the sorting order.

npx hdu -f -s asc output

And if we need only the first results, we can use the --head option.

npx hdu -f -s asc --head 3 output


Please try hdu, give it a star 🌟, and happy hacking! 💻

Credits

Photo by Art Wall - Kittenprint on Unsplash

Top comments (0)