Skip to content


Handy shell script

A while back I needed to free up some hard drive space. To free up the most space as quickly as possible, I wanted to focus on the larger directories first.

Of course there are a lot of utilities, like KDirStat, to do that sort of thing, but I didn’t need anything that fancy.

The solution I came up with is the following Shell/Perl one liner:

#!/usr/bin/zsh
ls -Q -d . | xargs du -b -- | \
sort -n |\
perl -e 'while (<>) { @p = split; $sz = shift @p;\
    printf("%3.2f MB \t@p\n",$sz/(1024*1024)); }'
view raw dud This Gist brought to you by GitHub.

I had to break it up into a few lines so it’d fit in the narrow column here, but it works just as well with the line breaks removed.

Here’s a sample run:

[jl2:debianlap ~/src/cgeom] dud
0.00 MB ./.git/branches
0.00 MB ./.git/objects/info
0.00 MB ./.git/refs/tags
0.00 MB ./.git/refs/remotes/origin
0.00 MB ./.git/refs/heads
0.00 MB ./.git/refs/remotes
0.00 MB ./.git/refs
0.00 MB ./.git/logs/refs/heads
0.00 MB ./.git/logs/refs
0.00 MB ./.git/info
0.00 MB ./.git/logs
0.00 MB ./datastructs/tests/__pycache__
0.01 MB ./svgimg
0.01 MB ./geom
0.01 MB ./ch2/tests
0.01 MB ./datastructs/__pycache__
0.01 MB ./datastructs/tests
0.01 MB ./ch1/tests
0.01 MB ./ch2
0.02 MB ./.git/hooks
0.02 MB ./.git/objects/pack
0.02 MB ./.git/objects
0.03 MB ./datastructs
0.04 MB ./.git
0.06 MB ./ch1
0.16 MB ./

I’ve found it to be 10x more useful than the regular “du” command, so I thought I’d put it up here in case anybody else wants to use it.

I could probably make it shorter using just Perl, but it doesn’t seem worth the effort, so I haven’t bothered.

Posted in coding, computers.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.