[SOLVED] How to sum bytes, kilobytes, megabytes and gigabytes using awk command
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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).
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.
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]); }'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.