LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 09-02-2011, 07:41 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Rep: Reputation: 127Reputation: 127
Convert du output to MB/GB...?


After calculating size with du for several folders, of about 600MB

giving me:
673996771


Code:
	TOTAL_SIZEH=`echo "$TOTAL_SIZE" | awk '{ sum=$1 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}'`
How could it be possible to convert it well to GB/MB, ... or anything for an human reading with a script in sh or awk, or sed... ?

thanks a lot !
 
Old 09-02-2011, 07:43 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
du -h not working for you?
 
Old 09-02-2011, 07:49 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by pwc101 View Post
du -h not working for you?
my list myfiles.lst contains:
Code:
directory1
directory2
directory3
myfile
....
could du read a given file and outputs its total content directly? Man du says that not
 
Old 09-02-2011, 07:56 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
This should work:
Code:
tr "\n" "\000" < myfiles.lst | du -h --files0-from=-
du can accept a file using the --files0-from flag, but it must be a null separated list. The tr command replaces all newlines in your myfiles.lst with null values which is then piped into du (with the useful -h flag). The filename - in the du command tells du to take the list of null separated files from standard input.

If you want the total for a directory, you probably want the -s flag too (du -sh --files0-from=-).
 
1 members found this post helpful.
Old 09-03-2011, 03:33 PM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by pwc101 View Post
This should work:
Code:
tr "\n" "\000" < myfiles.lst | du -h --files0-from=-
du can accept a file using the --files0-from flag, but it must be a null separated list. The tr command replaces all newlines in your myfiles.lst with null values which is then piped into du (with the useful -h flag). The filename - in the du command tells du to take the list of null separated files from standard input.

If you want the total for a directory, you probably want the -s flag too (du -sh --files0-from=-).
it is an amazing command. wow.

Sorry but however it does not summarize the total of size... Above into TOTAL_SIZEH my awk version.

However I guess one can do :
Code:
 tr "\n" "\000" < myfile |  du -b  --files0-from=- | awk ' { print $1 }  '
and then try to make the sum... then we got a Total of DU from any given files...
 
Old 09-03-2011, 07:13 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
I've never been a fan of "-h" (for "du") - messes with the output too much. I prefer to leave it as-is and sort it (numerically). Usually looking for the biggest space hog, so reverse sorted.
For the total, awk should do nicely - again, I just pick a unit of measure and use that.
KISS.
 
Old 09-04-2011, 04:24 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by syg00 View Post
I've never been a fan of "-h" (for "du") - messes with the output too much. I prefer to leave it as-is and sort it (numerically). Usually looking for the biggest space hog, so reverse sorted.
For the total, awk should do nicely - again, I just pick a unit of measure and use that.
KISS.
sort -h changed my life in situations such as those.
 
1 members found this post helpful.
Old 09-04-2011, 06:38 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Hey, you are *good* ....

I'll pinch that ... bloody manpages ...
 
Old 09-05-2011, 01:08 PM   #9
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Xeratul View Post
it is an amazing command. wow.

Sorry but however it does not summarize the total of size... Above into TOTAL_SIZEH my awk version.

However I guess one can do :
Code:
 tr "\n" "\000" < myfile |  du -b  --files0-from=- | awk ' { print $1 }  '
and then try to make the sum... then we got a Total of DU from any given files...
Try this:
Code:
tr "\n" "\000" < myfile | du -ch --files0-from=- | awk '/total$/ {print $1}'
That should give you the total for all files listed in myfile.
 
Old 09-05-2011, 08:21 PM   #10
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by pwc101 View Post
Try this:
Code:
tr "\n" "\000" < myfile | du -ch --files0-from=- | awk '/total$/ {print $1}'
That should give you the total for all files listed in myfile.
Wow. It is awesome. It really works, and it is accurate. It works.

I would like to thank you very much! link
 
  


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
How to convert Tcpdump output file to a Pcap format? gabriel01 Linux - Networking 6 11-26-2012 05:50 AM
Solved: Question about ImageMagick's convert utility and high quality output Dogs Linux - Software 6 06-25-2011 05:40 PM
FLAC files: to convert or not to convert to CD-DA. stf92 Linux - Software 2 12-24-2010 12:58 PM
[SOLVED] Using convert from imagemagick package to convert multiple jpg files janis_169 Linux - Newbie 5 07-18-2010 05:22 PM
convert cron output to plain text deesto Linux - General 5 01-09-2009 04:10 PM


All times are GMT -5. The time now is 08:43 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