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 04-25-2008, 09:12 AM   #1
paulBottomley
LQ Newbie
 
Registered: Oct 2005
Location: Canterbury, UK
Distribution: Fedora Core 4
Posts: 12

Rep: Reputation: 0
I need to calculate the sum of filesizes from a list of files


I need help - I have a file, called files, which contains a list of file paths in the format:

/media/hda5/Media/Music/TV theme tunes/Tin Tin.mp3

Yes, Tin Tin, so what :P

I would very much like to be able to calculate the total of all their filesizes. I got this code somewhere else online, which almost works except that awk isn't enjoying the spaces in the names, leading to thousands of "file not found" errors

files=`cat all_music`
total=0
for i in $files
do
size=`ls -l $i| awk '{print $5}'|sed 's/ //g'`
total=`expr $total + $size`
done
echo $total

I don't understand what I need sed for either.




On a slightly unrelated note, the file contains duplicates under different paths - is there a way to remove those from the file also?

Thank you in advance
 
Old 04-25-2008, 09:34 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Replace files=`cat all_music` by files=`cat all_music | sed 's,\(.*/\)\(.*\),\1@\2,' | sort -k 2 -u -t '@' | sed 's/@//g'`

This will remove the files with the same name under different paths.
 
Old 04-25-2008, 09:37 AM   #3
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
To solve the space problem:

total=0
cat all_music | sed 's,\(.*/\)\(.*\),\1@\2,' | sort -k 2 -u -t '@' | sed 's/@//g' | while read i
do
size=`ls -l "$i" | awk '{print $5}'|sed 's/ //g'`
total=`expr $total + $size`
done
echo $total
 
Old 04-25-2008, 10:16 AM   #4
paulBottomley
LQ Newbie
 
Registered: Oct 2005
Location: Canterbury, UK
Distribution: Fedora Core 4
Posts: 12

Original Poster
Rep: Reputation: 0
Wow, thanks for the quick reply. The code you provided solves the spaces problem, but now seems to expect a user input and always gives the total as 0. I think there's something up with "while read i", but I'm not sure what.

I played with things like read line, but they only served to really podge things up.

Also I realised that the same duplicate have separate filenames, annoyingly, so I guess I'll have to root them out longhand. Ah well, c'est la vie.
 
Old 04-25-2008, 10:36 AM   #5
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Removing duplicate will be harder then. Have to use cksum.
For now, you can simply do this:

total=0
while read i
do
size=`ls -l "$i" | awk '{print $5}'|sed 's/ //g'`
total=`expr $total + $size`
done < all_music
echo $total

Make sure you are using bash.
 
Old 04-25-2008, 01:19 PM   #6
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
Parsing the output of ls isn't the best way to do this, I'd use stat instead (that's what it's for afterall).

Code:
size=$(stat -c %s $file)
Actually, an easier way to be just to (unless you get a "too many arguments"):

Code:
du -c $(< files) | tail -n1
As for duplicate files, this might help: http://freshmeat.net/projects/songprint/

Last edited by ararus; 04-25-2008 at 01:20 PM.
 
  


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
printing filesizes greater than 1 mb sknowonweb Linux - Newbie 2 03-13-2008 11:16 AM
How can we list all the files used by a particular process indian Programming 1 01-05-2007 08:54 AM
lftp does not list files PhillipHuang Red Hat 1 12-28-2006 05:09 AM
OpenOffice exporting really huge PDF filesizes? R00ts Linux - Software 1 11-08-2004 06:05 AM
getting filesizes.. ichbinesderelch Linux - General 2 08-17-2003 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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