Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

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

  1. #11
    Join Date
    Jul 2010
    Beans
    6

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

    It is true that it is faster in the second run, but for a deep directory structure, it is still much slower than using only one run for the du command.

    I have used a command similar to #2 for a long time but was bothered about the slow speed.

    Well, yesterday I played around with awk which until yesterday has been black magic to me. I have used it several times in snippets found on the net without really understanding what it did, and how. But finally I saw the light!

    So I concocted this command which works well for me and only runs du once:

    Code:
    du --max-depth=0 -k * | sort -nr | awk '{ if($1>=1024*1024) {size=$1/1024/1024; unit="G"} else if($1>=1024) {size=$1/1024; unit="M"} else {size=$1; unit="K"}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res=sprintf(format,size,unit); printf "%-8s %s\n",res,$2 }'
    It works by NOT having du output data in human readable format, but instead letting awk prettify the output.

    Hope someone will find it useful. It took several hours to get it right, but now I master awk

  2. #12
    Join Date
    Jul 2006
    Location
    New York City
    Beans
    21
    Distro
    Ubuntu 11.04 Natty Narwhal

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

    Quote Originally Posted by jackashe View Post
    oh it just dawned on my what to do in the final case! When du is saying that the size is 0, there is no suffix, so perl is just using 0 as the byte order character. So I fixed this by changing that part to '%byte_order = ( G => -3, M => -2, K => -1, k => -1);
    The SI output also generates some sizes as decimals, e.g. 7.1MB. The pattern match for the numbers needs to be expanded to include an optional decimal point and more digits.
    So my regex there went from /(\d+)/ to /(\d+\.?\d*)/

    My final version:

    %byte_order = ( G => -3, M => -2, K => -1, k => -1);

    # to understand : read from "bottom" to "top"
    ## print list
    ## convert list of tuples into list of just zeroth element of tuple
    ## sort tuple list by byte_order hash of element one sub-sorted by direct compare of element two
    ## convert list into tuple list consisting of [actual element, match of du size char, match of du size number]
    ## read list from stdin
    print map { $_->[0] }
    sort { $byte_order{$a->[1]} <=> $byte_order{$b->[1]} || $b->[2] <=> $a->[2] }
    map { [ $_, /([MGKk])/, /(\d+\.?\d*)/ ] }
    <>;
    dell 1420n

  3. #13
    Join Date
    Sep 2007
    Beans
    37

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

    Quote Originally Posted by marlar View Post
    Well, yesterday I played around with awk which until yesterday has been black magic to me. I have used it several times in snippets found on the net without really understanding what it did, and how. But finally I saw the light!
    Nice marlar!! Awk is still black magic for me. Maybe one day I will learn it as well as you. Until then I use this version of the "run du twice" example posted by ghostdog74 which outputs virtually identical info to your awk idea (but not as "neatly"):

    Code:
    du -k --max-depth=1 | sort -nr | cut -f2 | xargs -d '\n' du -sh

  4. #14
    Join Date
    Feb 2009
    Location
    Mexico City
    Beans
    20
    Distro
    Ubuntu 11.04 Natty Narwhal

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

    I apologize for reviving an old thread, but this is the first result in a Google search for "du sort human size", so I thought I'd post the answer I found to this problem for any subsequent google searchers:

    Code:
    du -hs | sort -h
    However, this requires GNU coreutils >= 7.5, which should be available in most recent linux distros. It's available in Ubuntu since Maverick.

    The sort command now supports sorting "human-readable" size numbers through the -h option.

  5. #15
    Join Date
    Jun 2006
    Beans
    2,930

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

    Nice.
    Thanks for the update.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  6. #16
    Join Date
    Sep 2011
    Beans
    2

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

    Quote Originally Posted by meithan View Post
    I apologize for reviving an old thread, but this is the first result in a Google search for "du sort human size", so I thought I'd post the answer I found to this problem for any subsequent google searchers:

    Code:
    du -hs | sort -h
    However, this requires GNU coreutils >= 7.5, which should be available in most recent linux distros. It's available in Ubuntu since Maverick.

    The sort command now supports sorting "human-readable" size numbers through the -h option.
    Thanks a million. I had no idea. I had coreutils 8.10 (via MacPorts) and it didn't have this in the manpage. The other solutions had problems handling whitespace or execution time.

  7. #17
    Join Date
    Dec 2007
    Beans
    2

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

    find $RootDir -exec du -BK --max-depth=0 {} \; | perl -ne 'm/^(\d{0,10})([K|M|G]\s+)(.*?)(StringToStartTheRelativePathsFrom\.)(.*)/g; printf ("%7d %1s %-8s%-120s \n" , $1 , $2 , $4 , $5) ' | sort -nr >> $FileReportRelativePaths

  8. #18
    Join Date
    Mar 2010
    Beans
    3

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

    Hi all,

    In man sort I found this:
    POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more sin-
    gle-letter ordering options, which override global ordering options for that key. If no key is given, use the entire
    line as the key.

    SIZE may be followed by the following multiplicative suffixes: % 1% of memory, b 1, K 1024 (default), and so on for M,
    G, T, P, E, Z, Y.
    How can I use it to sort output of "du -hs ./*" ?
    I should set SIZE or POS variables or override global ordering? How I can do this?

  9. #19
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    13,406
    Distro
    Kubuntu 18.04 Bionic Beaver

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

    What's wrong with

    Code:
    du -s * | sort -n
    That does a numeric sort of the first field returned, in this case the size field.

  10. #20
    Join Date
    Feb 2009
    Beans
    1,468

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

    Quote Originally Posted by perat View Post
    Hi all,

    In man sort I found this:

    How can I use it to sort output of "du -hs ./*" ?
    I should set SIZE or POS variables or override global ordering? How I can do this?
    This thread is nearly a year old; you should ask your question in a new one if the solutions already proposed don't do what you want.

Page 2 of 3 FirstFirst 123 LastLast

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
  •