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
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    Degradable SELECT onChange

    Whenever I go to Google Analytics I notice a slight flicker in the dropdown list area. I see a button appear for the shortest amount of time and the poof! Gone. What that tells me is that Google is making their site function...

  • By
    Dijit’s TabContainer Layout:  Easy Tabbed Content

    One of Dojo's major advantages over other JavaScript toolkits is its Dijit library.  Dijit is a UI framework comprised of JavaScript widget classes, CSS files, and HTML templates.  One very useful layout class is the TabContainer.  TabContainer allows you to quickly create a tabbed content...

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!