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
    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...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Create a 3D Panorama Image with A-Frame

    In the five years I've been at Mozilla I've seen some awesome projects.  Some of them very popular, some of them very niche, but none of them has inspired me the way the MozVR team's work with WebVR and A-Frame project have. A-Frame is a community project...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

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!