Juhapekka Tolvanen wrote:
On Fri, 08 Jan 2010, +06:42:05 EET (UTC +0200), Matthew Woehlke wrote:
Juhapekka Tolvanen wrote:
On Thu, 31 Dec 2009, +14:51:44 EET (UTC +0200),
Eric Blake pressed some keys:

According to Juhapekka Tolvanen on 12/30/2009 4:35 PM:
I wanted to do this: Give sizes of each files and directories located
in $PWD in human-readable-format AND sort output according to sizes of
those files and directories.

What's wrong with:

du -sh0 * | sort -hz | tr '\0' '\n'

besides needing coreutils 7.5 or newer?

What if size of one directory is rounded UP to 1M and size of other
directory is rounded DOWN to 1M? How sort-command can know know, which
one is bigger? I am sure, that sorting must be done according to size
in bytes, not according to size of human-readable units.

Okay. What is wrong with:

du -B 1 -s -0 * \
   | sort -nz \
   | awk -v RS='\0' '{print $2}' \
   | xargs -d '\0' du -sh

?

It does not work for me:

Meh, guess I should try it. Should be:

du -B 1 -s -0 * \
    | sort -nz \
    | awk -v RS='\0' '{printf "%s\0", $2}' \
    | xargs -0 du -sh

But that doesn't work when files have spaces in them. (I took the '$2' from your original script, btw.)

You'd do better to replace the bits after 'sort' with:

awk -v RS='\0' '{
   if ( $1>  0x3F000000 )
     printf "%6.1fG\t", $1 / 0x40000000 ;
   else if ( $1>  0xF0000 )
     printf "%6.1fM\t", $1 / 0x100000 ;
   else if ( $1>  0x300 )
     printf "%6.1fK\t", $1 / 0x400 ;
   else
     printf "%6i\t", $1 ;
   $1="" ;
   print $0 ;
}'

This should output the file names without any sort of mangling (I think), but even if it does, it's just output at this stage; you aren't trying to re-stat the files, so you won't get errors even if mangling occurs.

How that awk-snippet is actually used? Can you provide it as a
shell-script-file?

You pipe 'du's output into it, i.e. 'du <args> | sort -nz | <snippet>'.

The attached script acts like 'du -h' with sorted output; some arguments might mess it up but generally you can pass other 'du' arguments to it (and of course file names).

--
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
--
If this message is intercepted, the sender will disavow all knowledge of its existence.
:
#kate: hl bash;

du -B 1 -0 "$@" | sort -nz | awk -v RS='\0' '
{
        if ( $1 > 0x3F000000 )
                printf "%6.1fG\t", $1 / 0x40000000 ;
        else if ( $1 > 0xF0000 )
                printf "%6.1fM\t", $1 / 0x100000 ;
        else if ( $1 > 0x300 )
                printf "%6.1fK\t", $1 / 0x400 ;
        else
                printf "%6i\t", $1 ;
        $1="" ;
        print $0 ;
}'

Reply via email to