I'm trying to print out a listing of all the files (and subdirectories) within a directory along with their sizes. I would like the output to be sorted by size (greatest to least), and in human-readable format.
EDIT on Jun 20, 2011: As posted by meithan, with GNU coreutils >= 7.5, which is available with Maverick and later, it's possible to do:
Read on for the old discussion.
Without that last restriction, I am able to generate a listing by running this command:
I can get the output in human-readable format by adding the -h switch to the du command; however, then the sort -nr command does not work properly (for example, it sorts 4.0K before 2.2M, because 4.0 is a bigger number than 2.2 -- it ignores the suffix).
serg@bijou:~/Pictures$ du -s ./* | sort -nr
Here's what I want the output to look like:
Unfortunately, this one had to be hand crafted -- I don't know how I could rewrite the command to get it to show this.
Can sed or awk be used to accomplish this? Or would I have to write a script? I'm only marginally familiar with any of these tools, so I would like some help.
Thank you in advance!