Get Directory Size from Shell

By  on  

My first goal when coding is usually to make it work and then clean up the code into a more maintainable format.  Many people prefer to keep code clean from start to finish but thinking "maintenance first" slows me down --- I like to GO GO GO!  Sometimes making code means the code size gets larger, sometimes it means getting smaller.

Oftentimes when I'm trying to abstract my CSS preprocessing code (Stylus), I abstract my mixins but want end up with the exact same CSS output.  What's the easiest way to check that the outcome is likely the same, without scouring the site and checking every section?  Checking the output directory size.  If the CSS code is the same size before and after changes, we're golden!  So how do we check directory size?

du -s 		# 16075464, recursive
du -sh 		# 7.7G, recursive, human readable

The commands above provide the directory size in different formats.  If you want to get the exact byte size of a directory, you can execute this slightly more complicated command:

find . -type f -exec ls -l {} \; | awk '{sum += $5} END {print sum}' # 8209358267

Of course something flukey could happen and the CSS change with the exact same size coming out, but this method isn't meant to be scientific.  And remember that these commands can be used for any number of purposes!

Recent Features

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • By
    Submit Button Enabling

    "Enabling" you ask? Yes. We all know how to disable the submit upon form submission and the reasons for doing so, but what about re-enabling the submit button after an allotted amount of time. After all, what if the user presses the "stop"...

Discussion

  1. JP

    I think your comments on du are misleading. du -s and du -sh both give you the “recursive” size of the directory. The -h flag just gives you a “human readable” value, i.e. in MB or GB.

  2. Lars

    You can also use du -h -s * to display the size of each sub-directory in a list format, which can be very handy

  3. This is a brilliant trick and a neat usage of bash commands. I am still impressed about how much they are “composable” to create complex logics.

    Thanks for sharing it David

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!