DEV Community

Cover image for Useful commands to check directories and files size
Paul Isaris
Paul Isaris

Posted on β€’ Edited on

33 6

Useful commands to check directories and files size

Often times, there is a need to check the size of directories or files in a project, or to evaluate the space a directory occupies in a live/development server, or just at our local machine.

Here is a list of useful commands that you can leverage to make sure the available disk space of your machine is normal and that the huge node_modules directory size of your project hasn't gone crazy :D

We will use the du command, which is explained below:

du (disc usage) command estimates file_path space usage

The options -sh are (from man du):

  -s, --summarize
         display only a total for each argument

  -h, --human-readable
         print sizes in human readable format (e.g., 1K 234M 2G)

To check more than one directory and see the total, use du -sch:

  -c, --total
         produce a grand total

So, let's see the basic commands:

Shows you the size of the directory in readable format:

$ du -sh directory/

Shows you a list with all the directories along with their sizes, ordered by size:

$ du -sh * | sort -h

Example in my example flutter project:

paul@paul-Inspiron-N5110:~/projects/flutter_test$ du -sh * | sort -h
4,0K    android.iml
4,0K    flutter_test_android.iml
4,0K    flutter_test.iml
4,0K    pubspec.yaml
4,0K    README.md
8,0K    test
12K lib
200K    android
212K    ios

Also, if you want to include hidden files and directories, you can run:

$ du -sch .[^.]* * |sort -h

Bonus:

Outputs the total and available size of your system's paritions

$ df -h

Outputs the inodes usage in your system:

$ df -i

If you have more useful commands to share, leave them in the comments below!

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (9)

Collapse
 
vinayhegde1990 profile image
Vinay Hegde β€’ β€’ Edited

While du is the good old-school way to learn, I prefer ncdu which from my experience is faster to calculate disk utilization and saves you the hassle of remembering or grepping the shell history to input multiple pipe-separated commands.

A lucidly explained guide on how to obtain & use it can be read here

Collapse
 
pavlosisaris profile image
Paul Isaris β€’ β€’ Edited

Thanks, haven't heard of that until now! I will definitely use it.

Collapse
 
phlash profile image
Phil Ashby β€’

If you're missing out on the fun in a Windows environment but have Powershell handy a colleague & I hacked together this equivalent a while back: gist.github.com/PhlashGBG/0a10d2c9...

Collapse
 
pavlosisaris profile image
Paul Isaris β€’

Nice tool, thanks!

Collapse
 
bambam82 profile image
Bart Dorlandt β€’

Do you have a trick to do this for hidden subdirectories as well? Without including the upstream directory?

Collapse
 
pavlosisaris profile image
Paul Isaris β€’

Hmm, I think you could use something like
du -sch .[!.]* * |sort -h

Collapse
 
bambam82 profile image
Bart Dorlandt β€’

Thanks Paul. It didn't work exactly (or at least not with zsh), but it got me going.

My result:

du -sch .[^.]* * |sort -h
Collapse
 
joshcheek profile image
Josh Cheek β€’

Great tips!

Collapse
 
pavlosisaris profile image
Paul Isaris β€’

Didn't know, that, thanks! :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay