Results 1 to 10 of 23

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

Hybrid 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.

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

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

    you can write you own routine, or if you insist on using du
    Code:
    # du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh

  3. #3
    Join Date
    Aug 2006
    Beans
    59

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

    Thank you very much, very clever! I understood each individual sub-command, but I wouldn't have thought of putting them together like this.

    The only change I made to make it do exactly what I wanted:
    du -ks * | sort -nr | cut -f2 | xargs -d '\n' du -sh
    Because I didn't want it to descend into subdirectories.

    Thank you very, very much!

  4. #4
    Join Date
    Oct 2009
    Beans
    1

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

    Thanks for this! Works great. A slight twist if you will from my own personal usage (aliases stem from my slackware days, old habits die hard):

    alias v='ls -lh'
    alias vv="du --max-depth=1 -k | sort -nr | cut -f2 | xargs -d '\n' du -sh"

    Cheers,

    jsaunders

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

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

    Quote Originally Posted by jsaunders View Post
    Thanks for this! Works great. A slight twist if you will from my own personal usage (aliases stem from my slackware days, old habits die hard):

    alias v='ls -lh'
    alias vv="du --max-depth=1 -k | sort -nr | cut -f2 | xargs -d '\n' du -sh"

    Cheers,

    jsaunders
    using vv|head gives a terminated by signal 13 message.
    This seems to take care of that:
    Code:
    alias vv="du --max-depth=1 -k | sort -nr | cut -f2 | xargs -d '\n' du -sh 2>/dev/null"
    I wrote a script before finding this thread to do a similar thing:
    Code:
    #!/bin/bash
    
    # du-sort : Provides a sorted du in human readable format
    
    # MAXDEPTH :  set to zero to show all
    MAXDIR=1
    
    # SORT ORDER  (true/false)
    ASCENDING=true
    
    # DEFAULT ARGUMENTS
    ARGS='-xh'
    
    # mktmp file
    DATA=$(mktemp)
    
    # ADD MAX-DEPTH ARG
    if [ "$MAXDIR" -gt "0" ]
    	then
    	ARGS="$ARGS --max-depth=$MAXDIR"
    fi
    
    # CHECK FOR EXTRA ARGUMENT
    if ! [ -z $1 ]
    then
    	ARGS="$ARGS $1"
    fi
    
    # RUN DU
    du $ARGS >$DATA 2>/dev/null
    
    # SORT DATA AND OUTPUT
    if $ASCENDING
    then
    	for P in K M G
    	do
    		grep -e "[0-9]$P" $DATA|sort -n
    	done
    
    else
    	for P in G M K
    	do
    		grep -e "[0-9]$P" $DATA|sort -nr
    	done
    fi
    
    #remove datafile
    rm $DATA
    Support 7z in default installs!!!: Click Here

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

  6. #6
    Join Date
    Sep 2008
    Beans
    2

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

    These all work but either require running du twice or using a temp file. Here is a way to do it with neither using perl and a Schwartzian Transform http://en.wikipedia.org/wiki/Schwartzian_transform

    du -shx | perl -e '%byte_order = ( G => 0, M => 1, K => 2 ); print map { $_->[0] } sort { $byte_order{$a->[1]} <=> $byte_order{$b->[1]} || $b->[2] <=> $a->[2] } map { [ $_, /([MGK])/, /(\d+)/ ] } <>'
    Last edited by fusiondog77; December 3rd, 2009 at 11:20 PM.

  7. #7
    Join Date
    Sep 2010
    Beans
    9
    Distro
    Ubuntu 11.04 Natty Narwhal

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


  8. #8
    Join Date
    Apr 2007
    Location
    Belgium
    Beans
    148
    Distro
    Ubuntu 12.04 Precise Pangolin

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

    +1

    Quote Originally Posted by garbelini View Post

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
  •