How to display disk usage by file type?

Basically I'm wondering where all my disk space is being eaten up on my drive and I would like to be able to analyze by file type

For example, I'd like to use the Terminal to see how much space is being used by the .psd files on my drive.

Is there a way to do such a thing?


Solution 1:

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.

Solution 2:

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