How to display disk usage by file type?

8,243

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
Share:
8,243

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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?

  • PAC
    PAC over 9 years
    Yes, I saw your answer perfect!
  • ulkas
    ulkas over 8 years
    how to sort the output by size?
  • OldGrampa
    OldGrampa almost 4 years
    To sort it by size, I added (just after =-) a space and then | sort -h
  • Danny Parker
    Danny Parker about 3 years
    A sorted version of this that works for macOS: gist.github.com/dannyNK/41f08ee7717d1b545a63c80ad61708a2