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…


This content originally appeared on DEV Community and was authored by Vladimir Vovk

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

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

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

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


This content originally appeared on DEV Community and was authored by Vladimir Vovk


Print Share Comment Cite Upload Translate Updates
APA

Vladimir Vovk | Sciencx (2024-09-22T22:01:42+00:00) Check Disk Usage with Terminal. Retrieved from https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/

MLA
" » Check Disk Usage with Terminal." Vladimir Vovk | Sciencx - Sunday September 22, 2024, https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/
HARVARD
Vladimir Vovk | Sciencx Sunday September 22, 2024 » Check Disk Usage with Terminal., viewed ,<https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/>
VANCOUVER
Vladimir Vovk | Sciencx - » Check Disk Usage with Terminal. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/
CHICAGO
" » Check Disk Usage with Terminal." Vladimir Vovk | Sciencx - Accessed . https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/
IEEE
" » Check Disk Usage with Terminal." Vladimir Vovk | Sciencx [Online]. Available: https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/. [Accessed: ]
rf:citation
» Check Disk Usage with Terminal | Vladimir Vovk | Sciencx | https://www.scien.cx/2024/09/22/check-disk-usage-with-terminal/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.