List Files in Human Readable Format

By  on  

I maintain an older computer with a small hard drive.  I use it mostly for storing MP3s, videos, and other types of basic media.  Unfortunately I often get warnings that disk space is low and so I need to delete files I no longer need; i.e. music or movies I have probably grown tired of.  I always sort them by size, largest first, so I can find out where the cogs are.

The problem is I try to stick to command line but only know how to do that sorting with human readable format by using Mac's Finder utility.  I recent found a command which will output the contents of the current directory and its subdirectories by human readable size:

du -sk -- * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1." ".$SI[((length $2)/3)]."\t":e'

That command is kinda gross so you'll probably want to save it as an alias.  Nonetheless I now know how to get the desired information from command line!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Implement the Google AJAX Search API

    Let's be honest...WordPress' search functionality isn't great. Let's be more honest...no search functionality is better than Google's. Luckily for us, Google provides an awesome method by which we can use their search for our own site: the Google AJAX Search API.

  • By
    JavaScript Speech Recognition

    Speech recognition software is becoming more and more important; it started (for me) with Siri on iOS, then Amazon's Echo, then my new Apple TV, and so on.  Speech recognition is so useful for not just us tech superstars but for people who either want to work "hands...

Discussion

  1. Han

    I use omnidisksweeper once in a while to get rid of junk

  2. Ayman Rady

    I don’t know if this is cross-platform or not, but on Ubuntu I use this

    # -h -- for human readable sizes
    du -sh ./* | sort -h
    

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