DEV Community

Serhii Klochko
Serhii Klochko

Posted on

4

How to check directory size in Linux/Ubuntu via command line

(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 .
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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 *
Enter fullscreen mode Exit fullscreen mode

Here it is! Now we see the size of each subfolder of 1st level.

du -sh .[^.]* *
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
kaamkiya profile image
Kaamkiya • • Edited

This is awesome! I had no idea this could be done so easily.
Is there a way to do this with files as well?

Collapse
 
graceman9 profile image
Serhii Klochko •

Thanks :) Yes this works for files too. But for files ls -lh is usually enough.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more