A command to list all file types and their average size in a directory

I am working on a specific project where I need to develop the composition of a large excerpt of documents so that we have a basic level for testing performance.

In particular, I need a command that can recursively go through the directory and for each type of file inform me about the number of files of this type and their average size.

I looked at solutions such as: Unix will find the average file size , How can I recursively print a list of files with file names less than 25 characters long using a single-line image? and https://unix.stackexchange.com/questions/63370/compute-average-file-size , but nothing brings me to what I need.

+4
source share
3 answers

This combination of du and awk should work for you:

du -a mydir/ | awk -F'[.[:space:]]' '/\.[a-zA-Z0-9]+$/ { a[$NF]+=$1; b[$NF]++ }
     END{for (i in a) print i, b[i], (a[i]/b[i])}' 
+5
source

Give you something to start, below the script you will get a list of files and their size line by line.

#!/usr/bin/env bash

DIR=ABC
cd $DIR

find . -type f |while read line
do 
  # size=$(stat --format="%s" $line)    # For the system with stat command
  size=$(perl -e 'print -s $ARGV[0],"\n"' $line )  # @Mark Setchell provided the command, but I have no osx system to test it. 
  echo $size $line 
done

Output sample

123 ./a.txt
23 ./fds/afdsf.jpg

Then this is your homework, with the output above, it will be easy for you to get the file type and average size

+2
source

"du", :

du -a -c *.txt

:

104 M1.txt
8   in.txt
8   keys.txt
8   text.txt
8   wordle.txt
136 total

512- , "-k" "-m".

0

Source: https://habr.com/ru/post/1526153/


All Articles