Recently I had to check how big the directory is. But not in the obvious way. The obvious way would be

du -h /tmp

The above command will output size of the catalog in human readable way (-h param) – in kB, MB, GB and so on. This is easy. But what if you want to check the size only for certain filetype within specified directory? It’s getting a little bit harder. To do that I prepared a command, the big one. Actually it’s not one command but five commands giving output to each other. It goes like this:

find /tmp -name '*.zip' 
    | xargs du 
    | awk '{sum[$3]+= $1;}END{for (s in sum){print sum[s], s;}}' 
    | sort -k2 
    | column -t

First command looks for all *.zip files in /tmp directory. Of course you can specify any rule which is acceptable by find command (search for „man find” in your favourite search engine) especially another filetype. Second command takes all found files and returns their sizes in bytes. Third command sums all of those sizes using awk (read more about it, it’s really powerful). Then, there is sorting by second column (indexes in -k argument goes from 1, not from 0) and after that there’s some pretty-printing. All you need to change (path and file type) is in the first command. If you need more explanation or some help, please leave a comment.

Enjoy 🙂