简体   繁体   中英

Linux Bash Scripting: Obtain the total size of 'png' and 'gif' files in a given Directory then compare the size

I'm new to Linux bash scripting in general and apologies in advance if this question is stupid.

Basically, I have this scenario where I need to obtain the total size of 'png' and 'jpeg' files in the same directory then afterwards compare this total size with a fixed value of (200kb). The problem im having is that I'm not sure how to obtain the total file size in the first place. Any help would be well appreciated..

What I've tried: (find the total file sizes of both jpeg and png files then save it inside variables

jpegsize = $({ find <DIR> -type f -name "*.jpg" -printf "%s+"; echo 0; } | bc)

pngsize = $({ find <DIR> -type f -name "*.png" -printf "%s+"; echo 0; } | bc)

totalsize = $jpegsize + $pngsize

if (($totalsize <= 200000))

then 

       echo "total image size is small"
else

then

       echo "total image size is NOT small"
fi

Your method for retrieving the size of jpg and png files seems correct (be careful though, there should be no space between the equals sign when assigning variables, see this answer ).


Here is the syntax for summing two variables:

totalsize=$((jpegsize + pngsize))

See this answer .


The final code:

#!/bin/bash

jpegsize=$({ find -type f -name "*.jpg" -printf "%s+"; echo 0; } | bc)

pngsize=$({ find -type f -name "*.png" -printf "%s+"; echo 0; } | bc)

totalsize=$((jpegsize + pngsize))

if (($totalsize <= 200000))
then
   echo "total image size is small"
else
   echo "total image size is NOT small"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM