You are not logged in.

#1 2014-09-30 19:50:01

predmijat
Member
Registered: 2014-09-30
Posts: 39

Sum of file sizes; based on file extension, sorted by size

Hello,

Here is what I need, hope someone can help me:
I want to get a sum of all file sizes in a current directory, based on the file extensions, e.g.

250K *.txt
800K *.php
2M    *.jpg
10M  *.mp4

I was trying something with "du -sch" and "type -f" but couldn't get a good result...

Thanks!

Offline

#2 2014-09-30 20:07:45

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,686
Website

Re: Sum of file sizes; based on file extension, sorted by size

I seem to remember finding a util that did just this... stay tuned, if I find it, I will post back.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2014-09-30 21:36:35

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Sum of file sizes; based on file extension, sorted by size

Offline

#4 2014-09-30 21:37:16

Saint0fCloud
Member
Registered: 2009-03-31
Posts: 137

Re: Sum of file sizes; based on file extension, sorted by size

If graysky doesn't find a tool, you could always do this in bash/zsh.

Step 1) Iterate through all files in whatever directory

for i in *; do

Step 2) Add the file size of each file to an associative array with the file extensions as the keys

((array[${i##*.}]+=$(du $i | cut -f 1)))

Step 3) Print your array and do whatever unit conversions necessary

Step 4) Profit

Offline

#5 2014-10-01 07:59:47

predmijat
Member
Registered: 2014-09-30
Posts: 39

Re: Sum of file sizes; based on file extension, sorted by size

Still not quite there, but here is what I came up with using bits from here and there:

#!/bin/bash

ftypes=$(find . -type f | grep -E ".*\.[a-zA-Z0-9]*$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq)

for ft in $ftypes
do
    echo -n "$ft "
    find . -name "*${ft}" -exec du -shc {} + | tail -1 | awk '{print $1}'
done

The output is:

.abc 3M
.bgh 150K
.cig 100M
.de 80K
...

but I can't the output as it as described in OP...

Offline

Board footer

Powered by FluxBB