How do I sort by human readable sizes numerically?
Solution 1
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
Solution 2
Try something like:
du -h folder | sort -h
Alternatives: -n
for Numerical sorting
Note: the -h
option of sort only exists in newer versions of Ubuntu.
Solution 3
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
Solution 4
Command GNU sort
has the following option:
-h
,--human-numeric-sort
compare human readable numbers (e.g., 2K 1G).To have this option on BSD/OSX, you can install
sort
fromcoreutils
(viabrew
) and add the bin folder to yourPATH
into your rc files.
So the command would looks like:
du -ah . | sort -rh | head -20
Solution 5
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.
Related videos on Youtube
UAdapter
Updated on September 18, 2022Comments
-
UAdapter almost 2 years
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
anddu -h folder
to see output sorted asdu folder
, but with first column fromdu -h folder
P.S. this is just an example. this technique might be very useful for me (if its possible)
-
SirCharlo almost 12 yearsI added the last part on how to make your solution into an alias.
-
Kat Amsterdam almost 12 yearsI'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]
-
Gaurav Bindal almost 12 years+1, similar one-liner:
du folder | sort -n | cut -f 2 | while IFS= read -r -d '' path; do du -sh -- "$path"; done
-
Gaurav Bindal almost 12 yearsthere is in version 8.17, so I guess this is the easiest way
-
Kat Amsterdam almost 12 yearsThanks 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.
-
Kat Amsterdam almost 12 yearsJust 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
-
jobin over 10 years: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 :)
-
mx7 over 10 yearsEvery work you did here worth complement.
-
muru over 9 yearsWhat's the point of replacing
\n
with\0
? Isn't it a bit too late for that? -
muru over 9 yearsI know about
-0
, but it's irrelevant: imgur.com/87w3vfj -
Elder Geek about 9 yearsDuplicate of answered Nov 17 '11 at 17:13 Allu2
-
yop83 over 6 yearsThis seems to work great on non-Linux systems which don't support GNU
sort
. -
Nam G VU about 5 yearsThis should be the accepted one cause it clean-shoot that resolve the OP with
sort -h
-
Nam G VU about 5 yearsWell askubuntu.com/a/80248/22308 simply do this with
sort -h