rc3.org

Strong opinions, weakly held

Unix command flag of the day

I just learned within the past 24 hours that the du command, which displays how much space a directory and all of its subdirectories are consuming supports the -h flag, which prints the results in a human-comprehensible format. Here’s the output of du -d 1 in my Web root:

7494 ./clips 18986 ./mt 145234 ./cache 236 ./planetrafe 794 ./test 518 ./ofinterest 4 ./cgi-bin 324 ./templates_c 14 ./images 3596 ./mt-static 706 ./2007 6 ./assets_c 178090 .

Here’s the output of du -d 1 -h:

7.3M ./clips 19M ./mt 142M ./cache 236K ./planetrafe 794K ./test 518K ./ofinterest 4.0K ./cgi-bin 324K ./templates_c 14K ./images 3.5M ./mt-static 706K ./2007 6.0K ./assets_c 174M .

If you use the Bash shell, you can save yourself some trouble by just creating an alias:

alias du=’du -h’

9 Comments

  1. The -h flag also works with the ls command.

    ls -lah

  2. Works for df, too.

    One of the things I love about UNIX is that being human-comprehensible is the non-default option.

  3. The problem with aliasing ‘du’ to ‘du -h’ is that then you can’t do ‘du -s * | sort -n’ to find the biggest directory.

    You can however alias ‘duh’ to ‘du -h’… 8^)=

  4. I actually did think about the command line scripting implications when I suggested creating the alias.

    I have always used ‘df -k’. I didn’t know about ‘df -h’.

  5. …and of course it doesn’t work on AIX. Nothing ever works on AIX, so I can never use neat tricks at work. I even had to learn vi, because that’s the only editor installed on half our systems.

  6. Somehow I never knew about -h until this day. Thanks.

  7. Maybe it’s just me but I’ve tried learning vi at least a dozen times over the past 10 years and every time I just give up in frustration. I usually end up resorting to pico or nano.

  8. I was pleased to learn about -h several months back, but what I learned from your post is “-d 1”. I’d always done stupid stuff like for x in ls -d; do du -sh $x; done.

    slaps forehead

    emacs emacs emacs

  9. Whoops, turns out “-d” doesn’t work on my du.

Leave a Reply

Your email address will not be published.

*

© 2016 rc3.org

Theme by Anders NorenUp ↑