How can I check the size of a file using Bash?
I've got a script that checks for 0-size, but I thought there must be an easier way to check for file sizes instead. I.e. file.txt
is normally 100 kB; how can I make a script check if it is less than 90 kB (including 0), and make it Wget a new copy because the file is corrupt in this case?
What I'm currently using...
if [ -n file.txt ]
then
echo "everything is good"
else
mail -s "file.txt size is zero, please fix. " [email protected] < /dev/null
# Grab wget as a fallback
wget -c https://www.server.org/file.txt -P /root/tmp --output-document=/root/tmp/file.txt
mv -f /root/tmp/file.txt /var/www/file.txt
fi
Solution 1:
[ -n file.txt ]
doesn't check its size. It checks that the string file.txt
is non-zero length, so it will always succeed.
If you want to say "size is non-zero", you need [ -s file.txt ]
.
To get a file's size, you can use wc -c
to get the size (file length) in bytes:
file=file.txt
minimumsize=90000
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize bytes
else
echo size is under $minimumsize bytes
fi
In this case, it sounds like that's what you want.
But FYI, if you want to know how much disk space the file is using, you could use du -k
to get the size (disk space used) in kilobytes:
file=file.txt
minimumsize=90
actualsize=$(du -k "$file" | cut -f 1)
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize kilobytes
else
echo size is under $minimumsize kilobytes
fi
If you need more control over the output format, you can also look at stat
. On Linux, you'd start with something like stat -c '%s' file.txt
, and on BSD and Mac OS X, something like stat -f '%z' file.txt
.
Solution 2:
stat
can also check the file size. Some methods are definitely better: using -s
to find out whether the file is empty or not is easier than anything else if that's all you want. And if you want to find files of a size, then find
is certainly the way to go.
I also like du
a lot to get file size in kb, but, for bytes, I'd use stat
:
size=$(stat -f%z $filename) # BSD stat
size=$(stat -c%s $filename) # GNU stat?
Solution 3:
An alternative solution with AWK and double parenthesis:
FILENAME=file.txt
SIZE=$(du -sb $FILENAME | awk '{ print $1 }')
if ((SIZE<90000)) ; then
echo "less";
else
echo "not less";
fi