The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help in sort ali560045 Shell Programming and Scripting 2 12-04-2007 07:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 06:56 AM
sort help unics Shell Programming and Scripting 14 04-30-2007 07:28 AM
How do i sort? abhijeetkul Shell Programming and Scripting 1 12-22-2005 03:49 AM
2nd sort key whatisthis UNIX for Dummies Questions & Answers 1 10-20-2004 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
du -h | sort ?

Hi all,

I want to sort a directory by file size, du -k |sort -nr is fine, but the output number is NOT friendly.
So how to sort more friendly with du -h ?
  #2 (permalink)  
Old 11-02-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
The problem with -h is that you end up with things like K, M and G for kB, MB, or GB (I think, I have no access to Linux right now to actually verify this). So you are better off using -k if you want to sort it.
  #3 (permalink)  
Old 11-02-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
I have an approach
du -k |sort -nr > sort_file.txt.

The output file will be like this:
3783749 .
5294 ./dir3
4790 ./dir3/dir5/dir1
3088 ./dir8
...

can you help me on how to convert the number from sort_file.txt to MB, GB format, like this:

3.6 GB .
5.2 MB ./dir3
4.7 MB ./dir3/dir5/dir1
3 MB ./dir8
...

Thanks
  #4 (permalink)  
Old 11-02-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
You can use awk to format the size field :
Code:
du -k | sort -nr | awk '
     BEGIN {
        split("KB,MB,GB,TB", Units, ",");
     }
     {
        u = 1;
        while ($1 >= 1024) {
           $1 = $1 / 1024;
           u += 1
        }
        $1 = sprintf("%.1f %s", $1, Units[u]);
        print $0;
     }
    ' > sort_file.txt

Jean-Pierre
  #5 (permalink)  
Old 11-02-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Thank Jean-Pierre,

your script with awk works, but the calculation is not correct.
My actual data like this:
126M /openoffice/bin
31M /openoffice/old_versions

but it report:
0.1 MB /openoffice/bin
0.0 MB /openoffice/old_versions

Could you please correct it!
Thank so much!
  #6 (permalink)  
Old 11-02-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
The awk script formats the result of the du -k command not du -h.


Jean-Pierre.
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0