[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A new solution for sorting du -h

From: Alan Evans
Subject: A new solution for sorting du -h
Date: Tue, 22 Feb 2011 14:51:05 -0500

There are many discussions about sorting the human readable form of du
for output in reports/cron jobs and the like.

Some people use perl or bash scripts.  Others use sort and then xargs
(which is silly IMO).  Yes, the first pass of du should prime caches
BUT if du is traversing a really large directory structure on a system
with little in the way of buffers the second du might end up
traversing the filesystem again.

A simple and elegant solution comes to mind.

Why not add a switch that outputs human readable AND block or byte or
some other non human from like the following:

5.4G    5642672 /home/alan/

Or even allowing multiple format specifiers to be applied resulting in
white space separated columns of output.

Or maybe allowing an arbitrary format specifier using printf or date
style substitution.

du --format="%h %k %n\n"

Then sorting is easy!

du -h --some-flag --max-depth=1 /home | sort -n -k2
- or -
du -h -k --max-depth=1 /home | sort -n -k2

And if you want to throw away the middle column.

du -h -k --max-depth=1 /home | sort -n -k2 | awk ' {print "$1\t$3" }'


reply via email to

[Prev in Thread] Current Thread [Next in Thread]