HackCode
HackCode

Reputation: 1857

How to calculate total size of all css,js and html pages separately uing shell script?

I have the following code in shell script

find -name "*.css" -exec -printf '%16f Size: %6s\n'

This gives me the file size of every css file. How do I modify this to get the added sum of all the file sizes ?

Upvotes: 1

Views: 194

Answers (2)

Srini
Srini

Reputation: 1

In 2 steps:

1) ll *css | tr -s " " > filename.txt 2) awk 'BEGIN {x=0} {x+=$5} END {print x}' filename.txt

Upvotes: 0

arco444
arco444

Reputation: 22841

You could use awk:

find . -name "*.css" -type f -printf '%s\n' | awk '{ tot+=$0 } END { print tot }'

Or in pure bash:

total=0
while read -r s; 
do 
  total=$(( total+s ))
done < <(find . -name "*.css" -type f -printf '%s\n')

echo $total

Upvotes: 3

Related Questions