Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 5 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
add comment

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
add comment
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.