Kusari icon

Code Composition Counter

Kusari | PRO | 04/09/21 02:58:25 AM UTC (Edited) | 0 ⭐ | 265 👁️ | Never ⏰ | []
Bash |

840 B

|

None

|

0 👍

/

0 👎

# Composition Counter
# Made by Gary
# Figures out the percentages of different file extensions and their sizes (similarly to what Github displays but for bash)
# Not really optimized that well but works fine
 
#!/bin/bash
 
shopt -s globstar nullglob dotglob
 
# First iteration to get total size
fullsize=0
for ext in c cpp txt asm inc md sh m4 s; do
    files=(**/*."$ext" )
 
    for f in "${files[@]}"; do
        fullsize=$(($fullsize + `stat --printf="%s" "%f"`))
    done
done
 
# Second iteration to get percentages
for ext in c cpp txt asm inc md sh m4 s; do
    totalsize=0
    files=(**/*."$ext" )
 
    for f in "${files[@]}"; do
        totalsize=$(($totalsize + `stat --printf="%s" "%f"`))
    done
 
    divided=`echo "scale=8 ; $totalsize / $fullsize * 100" | bc`
 
    printf '%s:\t%d (%d bytes, %f%%)\n' "$ext" "${#files[@]}" "$totalsize" "$divided"
done

Comments