Results 1 to 10 of 23

Thread: du question: sort by size & show human-readable sizes

Threaded View

  1. #1
    Join Date
    Aug 2006
    Beans
    59

    du question: sort by size & show human-readable sizes

    Hello,

    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:

    Code:
    du -hs | sort -h
    Read on for the old discussion.

    -----------------

    Without that last restriction, I am able to generate a listing by running this command:
    Code:
    serg@bijou:~/Pictures$ du -s ./* | sort -nr
    2172    ./Backgrounds
    1540    ./100_1065.jpg
    1448    ./100_1024.jpg
    1192    ./100_1033.jpg
    1136    ./100_1028.jpg
    1128    ./100_1068.jpg
    1088    ./100_1022.jpg
    1032    ./100_1027.jpg
    1016    ./100_1011.jpg
    1008    ./100_1025.jpg
    952    ./100_1021.jpg
    944    ./100_1061.jpg
    104    ./conference.jpg
    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).

    Here's what I want the output to look like:

    Code:
     2.2M    ./Backgrounds
    1.6M    ./100_1065.jpg
     1.5M    ./100_1024.jpg
     1.2M    ./100_1033.jpg
     1.2M    ./100_1028.jpg
     1.2M    ./100_1068.jpg
     1.1M    ./100_1022.jpg
     1.1M    ./100_1027.jpg
     1016K    ./100_1011.jpg
     1008K    ./100_1025.jpg
     952K    ./100_1021.jpg
     944K    ./100_1061.jpg
     104K    ./conference.jpg
    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!
    Last edited by svk; June 20th, 2011 at 07:13 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •