LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to sum bytes, kilobytes, megabytes and gigabytes using awk command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-sum-bytes-kilobytes-megabytes-and-gigabytes-using-awk-command-4175439404/)

roopakl 11-30-2012 01:10 AM

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

cat /tmp/test_file
24K
556M
1.2G
6.2M
17M
24K
30G
1.6G
28K

Now how to get the total size using awk command by reading each line of /tmp/test_file
Thanks in advance for your kind help.

druuna 11-30-2012 02:04 AM

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 }'
This uses du's -k option to force output to be in k bytes (blocksize is 1k).

roopakl 11-30-2012 03:02 AM

Thank you very much druuna,
Quote:

du -sk * | awk '{ total = total + $1 } END { print total }'
This worked great. But is there any way to convert total kb into human readable format i.e(kb/mg/gb) instead of manually counting like?
Code:

**,***,***
gb, mb, kb

I mean the total should show like
Code:

800 K
or
2.6 M
or
41 G etc.....


druuna 11-30-2012 03:34 AM

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"  }'
2082248 k - 2033.45 M - 1.98579 G

This shows the total (standard in k bytes), divided by 1024 to get Mb or devided by 1024^2 to get Gb.

roopakl 11-30-2012 04:02 AM

Thanks for your kind help druuna.
Code:

du -sk * | awk '{ total = total + $1 } END { print total " k - " total/1024 " M - " total/1024/1024 " G"  }'
Worked great. I request you to post awk and sed tutorials links. I know, I can google for it. But I wanted to know which sites you people would suggest.

Once again thank you very much druuna.

druuna 11-30-2012 04:22 AM

These are useful links in my opinion:

Sed/Awk resources:
Not awk/sed related, but useful nonetheless:

Bash resources:

roopakl 11-30-2012 04:31 AM

The above links are enough for me. I'll follow those. Thank you very much druuna, have a nice day:cool:

Habitual 11-30-2012 06:05 AM

Quote:

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...
since you mentioned "each directory", this may help.
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]); }'
output:

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

roopakl 11-30-2012 12:02 PM

Quote:

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]); }'
Excellent Habitual, Thank you very much.


All times are GMT -5. The time now is 05:19 AM.