#!/bin/bash # Copyright (c) 2004 Pekka "PQ" Paalanen # http://www.iki.fi/pq/ # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to # deal in the Software without restriction, including without limitation the # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or # sell copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # Version 14.6.2004 # function to transform a naked integer to a postfixed form function int2ki { declare -a pfix=( ki Mi Gi Ti Pi Ei insane ) declare -i i=-1 declare -i v=$1 while [ $v -gt 1023 ] do (( i++ )) (( v /= 1024 )) done if [ $i -ge 0 ]; then echo "$v${pfix[$i]}" else echo "$v" fi } if [ -z "$1" ]; then echo "Usage: $0 dir" echo "where 'dir' the directory to be scanned" exit 1 fi find "$1" -type f -printf "%s\n" | sort -n | ( # these are the bins: zero - bins[0] - ... - bins[last] - Inf # set these to whatever you like, keep the order declare -i -a bins=( 0 1024 4096 65536 1048576 \ 10485760 104857600 1073741824 ) declare -i -a counts declare -i i=0 declare -i binmax=$(( ${#bins[*]}-1 )) declare -i linecount=0 # read the file sizes from 'find' while read nro do (( linecount++ )) while [ $i -le $binmax ] && [ $nro -gt "${bins[i]}" ] do (( i++ )) done (( counts[i]++ )) done # show results declare -i rel lowbnd="0" echo "percent files file size" for ((i=0; i<=binmax; i++)) do rel=$((counts[i]*10000/linecount)) printf "%3d.%02d %6d %6s - %6s\n" $((rel/100)) $((rel%100)) \ "${counts[i]}" "$lowbnd" "$(int2ki ${bins[i]})" lowbnd=$(int2ki ${bins[i]}) done rel=$((counts[i]*10000/linecount)) printf "%3d.%02d %6d %6s -\n" $((rel/100)) $((rel%100)) \ "${counts[i]}" "$lowbnd" echo "Total: $linecount files" )