Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Tuesday, November 22, 2022

Find total size of files of an extension given as input in bash

 #!/bin/bash
echo "Please enter extension"
read ext
total=0
for i in $(pwd)/*.$ext

do

#filesize=$((`stat --printf="%s" $i`))
#filesize=$((`wc -c $i | awk '{print $1}'))
filesize=$((`wc -c $i | cut -d " " -f 1`))
total=$(($total+$filesize))
done

echo "The total size of $ext files in present directory is $total"

#alternate way


#!/usr/bin/bash
read -rep $'Please enter the extension\n' ext
total=0
for i in `ls`
do
if [[ "$i" == *"$ext"* ]]
then
#echo $i
total=$(($total + `du -k $i | cut -f 1`))
fi
done
echo "The total size of $ext files is $total"