Getting file size from OSX terminal is a pain. Actually it's a pain in all BSD variants.

We normally use ls -sh on Linux derivatives. But this command on Mac doesn't return a human readable value.

Alternatively I was using du tool for a long time. du -sh/du -h gives a nice human readable output. As a bonus point, it also measures size of directories.

But one drawback of this command is, it doesn't support tab auto completion. What I mean is after typing du -h and pressing tab won't suggest or complete files/directories in the current directory!

I was tweaking around today, and came up with a small solution. I also shortened the command only to dh. Only for checking files size du -h is a veery looong command for me.

Here is the snippet I wrote in my .zshrc file, and did the trick. It should also work on .bashrc as well.

function dh() {du -h $1;}
function _dh() {_files $PWD -/;}
compdef _dh dh

Save it to your rc file and reload the terminal. Hit dh and Tab, see the magic happen.




What's on your mind?


307 Comments