如何使用 shell 脚本分别计算所有 css,js 和 html 页面的总大小?

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

我在 shell 脚本中有以下代码

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

这给了我每个 css 文件的文件大小。我如何修改它以获得所有文件大小的总和?

您可以使用 awk:

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

或纯 bash:

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

echo $total

分两步:

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