DEV Community

Discussion on: How to get the size of a directory in Linux

Collapse
 
opfour profile image
Opfour

Use this expanded command to get a better view of sorted file sizes:

root@server# du -sk ./* | sort -nr | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

6.3G ./lib
2.1G ./share
320.8M ./src
246.9M ./bin
41.2M ./sbin
29.9M ./include
17.4M ./libexec
6.5M ./local
24K ./games
4K ./libx32
4K ./lib64
4K ./lib32
Total: 8.1G

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is a super nice one-liner! Bookmarked!

Collapse
 
opfour profile image
Opfour

Thanks man :D

Collapse
 
boiliev profile image
Boyan Iliev

This is sick!

Collapse
 
opfour profile image
Opfour

used it since like 2009 when I needed to seperate a large folder containing thousands of file :)