(Note: It works for MacOS too)
If you need to check the size of a directory on your computer, it is better to use special tools such as ncdu, dust or tree. I prefer ncdu - it does the job!
But, it is more common to use CLI if you work with a remote machine, often without a permission to install additional software into it. Then pre-installed tools come to rescue - du. See how I use it below.
du -h .
This gives us a list of all directories and subdirectories in current directory. That's not what we meant, huh?
Note: The dot character can be replaced with a relative or absolute directory path.
du -sh .
The option -s
stands for summary result. Now we see the entire folder size. But still, how to see each subfolder individually?
du -sh ./*
# or just:
du -sh *
Here it is! Now we see the size of each subfolder of 1st level.
du -sh .[^.]* *
Then you might want to see hidden folder and files too. A bit more complicated with regular expression.
Moreover, usually we would like to see result sorted, the largest elements come first:
du -sh * | sort -rh
The sort options -r
is for reverse, and -h
is for natural/numeric sort order.
These are just the basics, please share your favourite method in the comments.
Top comments (2)
This is awesome! I had no idea this could be done so easily.
Is there a way to do this with files as well?
Thanks :) Yes this works for files too. But for files
ls -lh
is usually enough.