Linux – Safe way to determine size of a file using unix tools

fileslinuxmac-osxunix

I wonder, what's the best way to determine the size of a file using common unix tools. I need to determine the size of a file in bytes in a shell script. The problem is, that the shell script needs to be portable across different operating systems like osx, irix, linux — that said: using the "stat" command may not work well, because the arguments required to get the result i want are different on almost every operating system.

I tried to use:

cat ... | wc -c

and while this seems to work quite well, i will probably get issues in a multibyte environment, won't i? So: what's a good way to do this?

Best Answer

For this purpose you can use the following:

du --block-size=1 filename

I have no idea about its portability. In command's man page it is listed to be in GNU coreutils package.

If you want to determine the size of the file, rather than the space it takes on disk, add --apparent-size.