Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, If I want to list and sort the current folder's content based on their sizes (big files first), I can use Code: du * | sort -nr and ...
  1. #1
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251

    sort human readable output from du

    Hi all,
    If I want to list and sort the current folder's content based on their sizes (big files first), I can use
    Code:
    du * | sort -nr
    and the sizes are displayed in KB's. If I want the sizes to be displayed in human-readable format, I can use option -h for the du command, but the sort command will not work correctly. For example, a 8.0K file will be listed before a 4.0M file. Is there any way to sort and the output correctly for human-readable format?

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If I were doing this, I'd probably do it in perl. The perl sort allows you to specify a comparison routine.

    The utility at MSORT also allows the user to write a comparison routine (as it turns out, also in perl), and one then avoids all the supporting infrastructure of an entire perl script.

    I think some versions of awk have a built-in sort operator, and then the conversion of items like "17K" and "33.8M" could be done in awk.

    Let us know what you finally do ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Thanks drl. I got the answer from
    du question: sort by size & show human-readable sizes - Ubuntu Forums

    Code:
    du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh
    Also folks are talking about adding human readable sorting support to command sort in
    GNU Coreutils feature request: sort by human-readable disk sizes - Justinsomnia

    It would be nice if we can use sort -h to sort output from du -h *

Posting Permissions

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