Try this:
file=/root/stacktrace.log
maxsize=100
if [ -f "$file" ] && [ -n "$(find "$file" -size +"$maxsize"c)" ]; then
mailx -s "File size is more than $maxsize" example@gmail.com < /dev/null
fi
This uses find to determine that the size is $maxsize
or larger, in this case 100 bytes. I also required [ -f "$file" ]
to ensure we're looking at a file rather than a directory so find's recursive search won't find a file inside that directory's structure that is sufficiently large.
BSD find and GNU find (but not Solaris find) support better units than just c
for "characters" (bytes). Try k
, M
, or G
like -size +"$maxsize"M
or else -size +$((maxsize*1048576))c
(Never mind that the syntax highlighting looks odd. You are allowed to nest one level of double quotes inside a "$(…)"
command substitution.)