LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-30-2012, 01:10 AM   #1
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Rep: Reputation: Disabled
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.
 
Old 11-30-2012, 02:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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).
 
1 members found this post helpful.
Old 11-30-2012, 03:02 AM   #3
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Original Poster
Rep: Reputation: Disabled
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.....
 
Old 11-30-2012, 03:34 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
 
2 members found this post helpful.
Old 11-30-2012, 04:02 AM   #5
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Original Poster
Rep: Reputation: Disabled
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.

Last edited by roopakl; 11-30-2012 at 04:05 AM.
 
Old 11-30-2012, 04:22 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
These are useful links in my opinion:

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

Bash resources:
 
2 members found this post helpful.
Old 11-30-2012, 04:31 AM   #7
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Original Poster
Rep: Reputation: Disabled
The above links are enough for me. I'll follow those. Thank you very much druuna, have a nice day
 
Old 11-30-2012, 06:05 AM   #8
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,254
Blog Entries: 11

Rep: Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289
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
 
1 members found this post helpful.
Old 11-30-2012, 12:02 PM   #9
roopakl
Member
 
Registered: Sep 2011
Posts: 95

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
weird awk output while trying to sum a field schneidz Programming 5 06-26-2012 11:29 PM
sum up values from each columns (awk) lcvs Linux - Newbie 10 06-20-2012 04:16 AM
awk's sum not working? grob115 Linux - Newbie 4 04-29-2010 12:44 PM
[SOLVED] awk - sum total if field = string schneidz Programming 12 03-20-2010 04:56 PM
awk merge and sum lines problem lalo4080 Programming 4 08-12-2008 10:21 AM


All times are GMT -5. The time now is 11:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration