Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

du -h | sort ?

Shell Programming and Scripting


Tags
linux

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-01-2006
fongthai fongthai is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 9 November 2016, 8:48 PM EST
Posts: 59
Thanks: 3
Thanked 0 Times in 0 Posts
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 ?
Sponsored Links
    #2  
Old Unix and Linux 11-02-2006
blowtorch's Unix or Linux Image
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 1 July 2016, 6:18 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 11-02-2006
fongthai fongthai is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 9 November 2016, 8:48 PM EST
Posts: 59
Thanks: 3
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 11-02-2006
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
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
The Following User Says Thank You to aigles For This Useful Post:
Neo (04-15-2015)
Sponsored Links
    #5  
Old Unix and Linux 11-02-2006
fongthai fongthai is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 9 November 2016, 8:48 PM EST
Posts: 59
Thanks: 3
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #6  
Old Unix and Linux 11-02-2006
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
The awk script formats the result of the du -k command not du -h.


Jean-Pierre.
Sponsored Links
    #7  
Old Unix and Linux 11-02-2006
fongthai fongthai is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 9 November 2016, 8:48 PM EST
Posts: 59
Thanks: 3
Thanked 0 Times in 0 Posts
Oh, yes.
It's perfect! Jean
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Alternate to sort --random-sort digitalviking Shell Programming and Scripting 5 04-14-2012 04:07 PM
Help to sort out... Possible use of sort command dnam9917 Shell Programming and Scripting 5 02-04-2012 01:08 PM
Is it Possible to sort a list of hexadecimal numbers using "sort" command? Kesavan Shell Programming and Scripting 9 08-21-2011 08:33 AM
Script to sort the files and append the extension .sort to the sorted version of the file pankaj80 UNIX for Advanced & Expert Users 3 06-07-2011 09:28 AM
How to Sort Floating Numbers Using the Sort Command? daniel.gbaena Shell Programming and Scripting 7 05-16-2010 11:20 AM



All times are GMT -4. The time now is 09:34 AM.