Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

for example I have command that shows how much space folder takes

du folder | sort -n

it works great, however I would like to have human readable form

du -h folder

however if I do that than I cannot sort it as numeric.

How to join du folder and du -h folder to see output sorted as du folder, but with first column from du -h folder

P.S. this is just an example. this technique might be very useful for me (if its possible)

share|improve this question

7 Answers 7

up vote 7 down vote accepted

Here is a more general approach. Get the output of du folder and du -h folder in two different files.

du folder > file1
du -h folder > file2

The key part is this: concatenate file1 and file2 line by line, with a suitable delimiter.

paste -d '#' file1 file2 > file3

(assuming # does not appear in file1 and file2)

Now sort file3. Note that this will sort based on file1 contents and break ties by file2 contents. Extract the relevant result using cut:

sort -n -k1,7 file3 | cut -d '#' -f 2

Also take a look at man sort for other options.


You may also save this as an alias, for later re-use. To do so, add the following to the bottom of ~/.bashrc:

sorted-du () {
    paste -d '#' <( du "$1" ) <( du -h "$1" ) | sort -n -k1,7 | cut -d '#' -f 2
}

Then, open a new terminal session and execute your new alias:

sorted-du /home
share|improve this answer
    
I added the last part on how to make your solution into an alias. –  SirCharlo Aug 30 '12 at 17:50
    
Using actual files in an alias (and then calling rm on them) is a really really bad idea! Think about what would happen if I actually had a file named file3 in my directory, it would be overwritten and then deleted! @SirCharlo I am removing the alias definition from this answer and replacing it with something safer. –  jmiserez Dec 31 '13 at 11:10

Something like:

 du -h folder | sort -h

Result:

 user@hostname:~$ du -h /var | sort -h > du: invalid option -- 'h' > Type 'sort --help' for more information.

Alternatives: -n Numerical sorting

note: the -h option of sort only exists in newer versions of Ubuntu.

share|improve this answer
1  
there is in version 8.17, so I guess this is the easiest way –  steabert Aug 30 '12 at 18:59
1  
Thanks to steabert for pointing out that the sort command has been improved from sort --version = 8.17 This is the best answer for newer versions of Ubuntu. –  Damiön la Bagh Aug 30 '12 at 19:07

This answer is valid for 10.04.4LTS and lower versions of Ubuntu.

Unfortunatly the accurate answer which sorts K M G is difficult and complex:

You can alias the entire du command with one that sorts human readable using this

alias duf='du -sk * | sort -n | perl -ne '\''($s,$f)=split(m{\t});for (qw(K M G)) {if($s<1024) {printf("%.1f",$s);print "$_\t$f"; last};$s=$s/1024}'\'

which I found here

http://www.earthinfo.org/linux-disk-usage-sorted-by-size-and-human-readable/

just cd into the folder you would like to know then duf

you could add this duf alias to the end of your /home/user/.profile to make the duf command semi-permenant

results:

user@hostname:~$ duf
0.0K  Documenten
0.0K  Muziek
0.0K  Openbaar
0.0K  Sjablonen
0.0K  Video's
4.0K  backup_db.sql.g
4.0K  examples.desktop
12.0K xml printer ticket
52.0K hardinfo_report.html
152.0K    librxtxSerial.so
2.7M  jpos
4.4M  nxclient_3.5.0-7_amd64.deb
6.4M  nxnode_3.5.0-4_amd64.deb
6.8M  Downloads
7.4M  nxserver_3.5.0-5_amd64.deb
12.4M NetBeansProjects
18.1M mysqlworkbench.deb
28.3M Afbeeldingen
45.8M ergens-20110928-18.sql.gz
60.5M 2012-06-02ergens_archive.tar.gz
65.5M 2012-08-26ergens_archive.tar.gz
65.6M 2012-08-28ergens_archive.tar.gz
65.6M 2012-08-29ergens_archive.tar.gz
65.7M 2012-08-30ergens_archive.tar.gz
113.0M    Bureaublad
306.2M    ergens-20110928-18.sql

Here is why du -sch /var/* | sort -n does not work see the sorting of MKKMMKKMMK

user@hostname:~$ du -sch /var/* |sort -n

0 /var/crash
0 /var/local
0 /var/lock
0 /var/opt
8,0M  /var/backups
12K   /var/games
16K   /var/tmp
17M   /var/log
68M   /var/cache
104K  /var/spool
144K  /var/run
351M  /var/lib
443M  totaal
704K  /var/mail
share|improve this answer
    
I've reported this to the gnu-core developers with a feature request to improve the du -h function with a sort feature. via [email protected] –  Damiön la Bagh Aug 30 '12 at 18:40
    
+1, similar one-liner: du folder | sort -n | cut -f 2 | while IFS= read -r -d '' path; do du -sh -- "$path"; done –  steabert Aug 30 '12 at 18:55
    
Just tried your 1 liner on a live system. Get a Access Denied error and no du results. Did you try this command on an Ubuntu system first? I changed folder to /home/username –  Damiön la Bagh Aug 30 '12 at 19:11
    
oh, sorry, you should drop the -d '', that's a left-over from using it on the output of the file command. –  steabert Aug 30 '12 at 19:43

For recent versions of Ubuntu, use du -h directory | sort -h .

I use a form of this all the time for finding out-of-control files.

share|improve this answer

This one handles filenames with whitespace or apostrophes, and works on systems which do not support xargs -d or sort -h:

du -s * | sort -n | cut -f2 | tr '\n' '\0' | xargs -0 -I {} du -sh "{}"

which results in:

368K    diskmanagementd
392K    racoon
468K    coreaudiod
472K    securityd
660K    sshd
3.6M    php-fpm
share|improve this answer
    
What's the point of replacing \n with \0? Isn't it a bit too late for that? –  muru 16 hours ago
    
it prepares the input for xargs -0, which expects the input to be NUL delimited (as find -print0 would produce). Otherwise you can’t use -0, and apostrophes have special meaning. –  Mark Crossfield 16 hours ago
    
I know about -0, but it's irrelevant: imgur.com/87w3vfj –  muru 16 hours ago

Open your terminal with Ctrl+Alt+t and type:

  du -sk * | sort -rn 
share|improve this answer
    
@Jobin thanks man . You have lot of patience –  AgentCool Dec 23 '13 at 15:43
    
:D I am now used to changing the Ctrl+Alt+t to the format above so much so that I have pasted the required format to my Xpad. :D Feels great to be complimented for such a menial job :) –  i08in Dec 23 '13 at 15:47
    
Every work you did here worth complement. –  AgentCool Dec 23 '13 at 16:27

The one I found working on all systems regardless of the version (sort -h doesn't work for me) is

du -h $(du * | sort -n | awk '{$2}')

the awk '{$2}' basically prints file names.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.