How to display disk usage by file type?

Try this:

find . -iname '*.psd' -print0 | du -ch --files0-from=-
  • find . -iname '*.psd' finds all files that end with an extension of psd
  • -print0 prints the file names followed by a null character instead of a newline
  • | du -ch --files0-from=- takes the file names from find and computes the disk usage. The options tell du to:
    • compute the disk usage of file names separated by a null character from stdin (--files0-from=-),
    • print sizes in a human readable format (-h), and
    • print a total in the end (-c).

Change .psd to whatever file type you want to find the disk usage for.


More generically, you could use a combination of find and awk to report disk usage grouping by any rule you choose. Here's a command that groups by file extensions (whatever appears after the final period):

# output pairs in the format: `filename size`.
# I used `nawk` because it's faster.
find -type f -printf '%f %s\n' | nawk '
  {
    split($1, a, ".");       # first token is filename
    ext = a[length(a)];      # only take the extension part of the filename
    size = $2;               # second token is file size
    total_size[ext] += size; # sum file sizes by extension
  }
  END {
    # print sums
    for (ext in total_size) {
      print ext, total_size[ext];
    }
  }'

Would produce something like

wav 78167606
psd 285955905
txt 13160