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

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Scrolling “Agree to Terms” Component with MooTools ScrollSpy

    Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...

  • By
    Jack Rugile’s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

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!