I'm looking for a simpler way to have a cross-os-unix file size check. I could use wc -c but I'm concerned the performance may suck on large files (I'm assuming it just counts chars and doesn't do a stat under the covers?)The below works for linux and macos (perhaps bsd). Is there a simpler well performing approach?function filesize{    local file=$1    size=`stat -c %s $file 2>/dev/null` # linux    if [ $? -eq 0 ]; then        echo $size        return 0    fi    eval $(stat -s $file) # macos    if [ $? -eq 0 ]; then        echo $st_size        return 0    fi    echo 0    return -1}
efficient cross-os file size shell function
bash;shell script;files
From the source of wc (coreutils/src/wc.c) in GNU coreutils (i.e. the verion on non-embedded Linux and Cygwin): When counting only bytes, save some line- and word-counting overhead.  If FD is a 'regular' Unix file, using lseek is enough to get its 'size' in bytes.So using wc -c to count the bytes will perform well.You can easily test this optimisation on a large file (i.e. one that would take some time reading). wc -c on a 9.9Gb file took 0.015s of real time on a file that is located on my server and I would rejoice if the whole file would have been transferred in that time, but my gigabit ethernet is unfortunately not that fast (it takes 21s to copy that file to /dev/null over the network).