Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to find out the total size of all files within a directory that have a particular extension.

I do some offsite backup via rsync but due to limited bandwidth and disk space at the other end I can't do everything, so I'd like to find out, for example, how much disk space MP3 files take up so I can decide whether to remove the mp3 extension from the current list of rsync excluded patterns.

It's not as simple as doing a 'du -sh' on the My Music directory in there as there's some other file types.

Thanks!

share|improve this question
up vote 7 down vote accepted

You can use du:

find Music/ -type f -name "*.mp3" -exec du -shc {} + | tail -1 | awk '{print $1}'

output example:

980M
share|improve this answer
    
I tried this on 100K+ text files and got 26M when I expected around 4G, but this answer worked better: stackoverflow.com/a/599079/172602 – Charlie Gorichanaz Nov 5 '13 at 7:01

I just tried the following

find Music/ -name '*.mp3' -exec ls -l {} \; | awk '{ SUM += $5} END { print SUM/1024/1024 }'

And got the correct answer in Megabytes (1024 x 1024)

share|improve this answer
    
Right on, works great. – Jeffrey Vandenborne Dec 26 '09 at 23:44
find Music/ -iname "*.mp3" -type f -exec stat -c "%s" {} \; | awk '{SUM+=$0} END {print SUM/1024/1024}'

Got the correct answer as well, exactly the same as pavium's solution, could be a bit more reliable though.

share|improve this answer

OK this is the best way I have seen which seems to execute orders of magnitude faster than ones containing awk:

find "My Music/" -type f -name "*.mp3" -print0 | du -hc --files0-from=- | tail -n 1

output:

1.3G Total

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .