Find the total size of certain files within a directory branch

find ./photos/john_doe -type f -name '*.jpg' -exec du -ch {} + | grep total$

If more than one invocation of du is required because the file list is very long, multiple totals will be reported and need to be summed.


du -ch public_html/images/*.jpg | grep total
20M total

gives me the total usage of my .jpg files in this directory.

To deal with multiple directories you'd probably have to combine this with find somehow.

You might find du command examples useful (it also includes find)


Primarily, you need two things:

  • the -c option to du, to tell it to produce a grand total;
  • either ** (activation instructions) or find (example) or to traverse subdirectories.
du -ch -- **/*.jpg | tail -n 1