How to check size of a file using Bash?
[ -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/Mac OS X, something like stat -f '%z' file.txt
.
It surprises me that no one mentioned stat
to check 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?
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