Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have 357 .png files located in different sub dirs of the current dir:

settings# find . -name \*.png |wc -l
    357

settings# find . -name \*.png | head
./assets/authenticationIcons/audio.png
./assets/authenticationIcons/bbid.png
./assets/authenticationIcons/camera.png
./bin/icons/ca_video_chat.png
./bin/icons/ca_voice_control.png
./bin/icons/ca_vpn.png
./bin/icons/ca_wifi.png

Is there a oneliner to calculate the total disk space occupied by them (before I pngcrush them)?

I've tried (unsuccessfully):

settings# find . -name \*.png | xargs du -s
4       ./assets/support/wifi_locked_icon_white.png
1       ./assets/support/wifi_vpn_icon_connected.png
1       ./assets/support/wi_fi.png
1       ./assets/support/wi_fi_conected.png
8       ./bin/blackberry-tablet-icon.png
2       ./bin/icons/ca_about.png
2       ./bin/icons/ca_accessibility.png
2       ./bin/icons/ca_accounts.png
2       ./bin/icons/ca_airplane_mode.png
2       ./bin/icons/ca_application_permissions.png
1       ./bin/icons/ca_balance.png
share|improve this question

4 Answers 4

up vote 2 down vote accepted

Maybe something like:

find . -type f -name "*.png" -printf '%s\n' | awk '{ sum += $1 } END { print sum }'

? Seems to work for me - results would be in bytes.

share|improve this answer
    
This is quite expensive, since du (an external command) is executed once for every file. –  adaptr Jul 10 '12 at 13:53
    
No need to pipe to cut -f1. –  quanta Jul 10 '12 at 13:56
    
@quanta: Doh, you're right :) –  thinice Jul 10 '12 at 14:29
    
@adaptr A new approach vs. du - as I agree, but he said he didn't have many files so I figured efficiency wouldn't matter. –  thinice Jul 10 '12 at 14:39

du is my favorite answer. If you have a fixed filesystem structure, you can use:

du -hc *.png

If you need to add subdirs, just add:

du -hc *.png **/*.png **/**/*.png

etc etc

However, this isn't a very useful command, so using your find:

TOTAL=0;for I in $(find . -name \*.png); do  TOTAL=$((TOTAL+$(du $I | awk '{print $1}'))); done; echo $TOTAL

That will echo the total size in bytes of all of the files you find.

Hope that helps.

share|improve this answer

To find the size in Kbytes (as long as column 5 of ls -l is 'size'

find . -name \*.png -exec ls -lk {} \; | awk '{ sum +=$5} END {print sum}'
share|improve this answer
1  
DO NOT try to parse the output of ls! It represents filesystem content, it is not actual filesystem content. –  adaptr Jul 10 '12 at 13:51
    
Yet we dont need to measure the filesystem content, we need to just sum the representation. –  Sirch Jul 10 '12 at 13:59
    
That's where the difference lies between the output from ls and du. –  adaptr Jul 10 '12 at 14:54

find . -name *.png -print0 | du -c --files0-from=- | tail -1

In logical block sizes; insert -b in the du command to output bytes.

share|improve this answer
    
Lack of the quotes around the file name. –  quanta Jul 10 '12 at 13:55
    
Irrelevant when using NUL-terminated filenames. –  adaptr Jul 10 '12 at 13:56
    
Don't you get find: paths must precede expression...? –  quanta Jul 10 '12 at 14:08
    
Only if you typo'ed my solution. –  adaptr Jul 10 '12 at 14:53
    
Do you want to see a screenshot? –  quanta Jul 10 '12 at 15:02

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.