0

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
Alexander Farber
  • 714
  • 4
  • 17
  • 38

4 Answers4

2

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.

thinice
  • 4,716
  • 21
  • 38
1

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.

MaddHacker
  • 306
  • 1
  • 3
  • 9
-1

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}'
Sirch
  • 5,785
  • 4
  • 20
  • 36
-1

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

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

adaptr
  • 16,576
  • 23
  • 34