How to sum bytes, kilobytes, megabytes and gigabytes using awk command
I know if we do du -chs /home/* we can get the size of each directory and also total size in the end. But also would like to confirm the total size with awk command, But I don't know how to? So any help would really be apreciated.
Here is is output of Code:
du -sh /home/* | awk '{ print $1 }' > /tmp/test_file Thanks in advance for your kind help. |
The problem is du's -h switch, which prints human readable output (4k 1.8G etc). Besides that a computer isn't to good with the human readable output, it also round down/up the values shown so the end result isn't as accurate as can be.
Would this help: Code:
du -sk * | awk '{ total = total + $1 } END { print total }' |
Thank you very much druuna,
Quote:
Code:
**,***,*** Code:
800 K |
You can do all sorts of arithmetic with the total variable:
Code:
$ du -sk * | awk '{ total = total + $1 } END { print total " k - " total/1024 " M - " total/1024**2 " G" }' |
Thanks for your kind help druuna.
Code:
du -sk * | awk '{ total = total + $1 } END { print total " k - " total/1024 " M - " total/1024/1024 " G" }' Once again thank you very much druuna. |
These are useful links in my opinion:
Sed/Awk resources: Not awk/sed related, but useful nonetheless: Bash resources: |
The above links are enough for me. I'll follow those. Thank you very much druuna, have a nice day:cool:
|
Quote:
Code:
du -sk ./* | /usr/bin/sort -n | /usr/bin/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]); }' 4K ./Public 4K ./x 4K ./x~ 12K ./Templates 32K ./Desktop 7.5M ./Videos 44.3M ./Bin 154.5M ./Pictures 4.5G ./Documents 6.1G ./Music 7.7G ./VirtualBox 8.7G ./Downloads Total: 24.4G |
Quote:
|
All times are GMT -5. The time now is 01:46 AM. |