Skip to content
Advertisement

Is there an easy way to aggregate disk usage by file type in a directory?

I have a folder, which contains 10 folders, which contain a huge mess of files. I’d like to see, not which files specifically are using the most, but which kinds of files (.png, .jpg, .txt etc.) are using the most disk space. I saw a previous post that listed the disk usage of a file type by giving the extension, but I want to show it for all extensions that exist in my file set, which makes it difficult to do.

This is on a bog-standard Debian installation, and I do not have permission to install new utilities.

Bonus points if it can be run from the folder containing 10 folders and show it for all 10.

Advertisement

Answer

May not be the most optimized way but should do the job:

#!/bin/bash

for ext in `find . -type f | perl -ne 'print $1 if m/.([^./]+)$/' | sort -u`; do
    echo $ext": "`find . -name "*."$ext -print0 | du -ch --files0-from=- | tail -1`
done
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement