4

I would like to know if there's a command that can tell me how much storage are the .jpg files (for example). Something like if I do find / -iname "*.jpg" which can add the size of each file found and output a total.

  • Output the find results in a file an run du -s on its content. – Julie Pelletier Jul 2 '16 at 20:05
5
find ./path/to/your/drive -type f -name '*.jpg' -exec du -ch {} +

Or much faster

find /path/to/your/drive -name "*.jpg" -print0 | du -ch --files0-from=-

Or simply,

du -ch /path/to/your/drive/*.jpg | grep total

Or with help of awk,

find /path/to/your/drive -iname "*.jpg" -ls | awk '{total += $7} END {print total}'

On my system file size shows on seventh field, if it's different for you then adjust accordingly.

As requested by OP in comment, if you want to find all images from a directory and total size you can use this command (suggested by @Stéphane Chazelas)

 find . -type f -exec file --mime-type {} + | sed -n 's|: image/[^[:blank:]]*$||p' | tr '\n' '\0' | du --files0-from=- -hc

Or

 du -shc $(find . -name '*' -exec file {} \; | grep -o -P '^.+: \w+ image' | cut -d':' -f1) | grep total
  • 1
    That will only give the individual sizes of each file. – Julie Pelletier Jul 2 '16 at 20:17
  • @julipelletier typo corrected – Rahul Jul 2 '16 at 20:19
  • Thank you Rahul, I just wonder how can I make it work with: find . -name '*' -exec file {} \; | grep -o -P '^.+: \w+ image' which basically looks for all images not just jp*g files. – VaTo Jul 2 '16 at 20:39
  • 2
    @Saul, find . -type f -exec file --mime-type {} + | sed -n 's|: image/[^[:blank:]]*$||p' | tr '\n' '\0' | du --files0-from=- -hc – Stéphane Chazelas Jul 2 '16 at 20:46
  • @Saul see updated answer – Rahul Jul 2 '16 at 21:08
2

As an alternative, you can do this using only POSIX:

find . -type f -name "*.jpg" -exec du -sk {} \; |
  awk 'BEGIN{total=0};{total += $1}; END{printf "%.3f MB\n", total / 1024}'

Further reading:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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