Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Is something like perl, python or tcl installed on all systems? –  jftuga Dec 19 '11 at 15:56
    
@jftuga: i have to assume, that no such is available –  harald Dec 19 '11 at 16:21
    
What are you actually trying to achieve.? Why do you need to know the file size and what is important about the file size? –  Iain Dec 20 '11 at 8:12
    
@lain: the filesize is part of a protocol specification i want to implement using the bash and standard unix tools. while i admit that this might not be the ideal tools for this, i would like to keep the requirements on the machine as low as possible. –  harald Dec 20 '11 at 8:20
add comment

5 Answers

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.

share|improve this answer
    
ls -l file -rw-r--r--. 1 iain iain 26976 Jul 13 14:46 file du -s file 32 file I think you meant du -b –  Iain Dec 19 '11 at 12:25
    
the problem with du is, that it counts blocks rather than bytes. but thanks anyway! –  harald Dec 19 '11 at 12:30
1  
as Iain mentioned -b will get number of bytes. –  Can Kavaklıoğlu Dec 19 '11 at 13:52
1  
OS X's version of du doesn't understand either -b or --block-size=1 (and BLOCKSIZE=1 du gives the error du: minimum blocksize is 512). –  Gordon Davisson Dec 19 '11 at 20:36
add comment

cksum FILE

From wikipedia

Interoperability

The standard cksum command, as found on most UNIX-like OS (including GNU/Linux, *BSD, Mac OS X, and Solaris) uses a CRC algorithm based on the ethernet standard frame check and is therefore interoperable between implementations

of course the interoperability is mentioned for the checksum and NOT for the counting bytes part.

share|improve this answer
1  
cksum seems indeed to be a very useful program. I did not know of this. I've just tested and at least on osx, linux and irix the output is identical. Thanks! –  harald Dec 19 '11 at 17:36
add comment

stat -c %s /path/to/filename

share|improve this answer
1  
on osx stat has no such parameter "-c" and on irix "-c" has a different meaning, so this is no solution for me. –  harald Dec 19 '11 at 13:39
add comment

ls and awk are pretty standard. You can try

ls -l file | awk '{print $5}'
share|improve this answer
    
sometimes, things are way more easy than say look at first sight. thanks very much :) –  harald Dec 19 '11 at 12:29
1  
ls outputs a representation of files, not an actual file list. The output should never be interpreted by shell scripts. –  adaptr Dec 19 '11 at 13:06
    
@adaptr: but what is the solution, than? –  harald Dec 19 '11 at 13:40
    
I wish I had an easy one for you :( I merely meant that the output from ls should not be considered dependable either. –  adaptr Dec 19 '11 at 13:44
1  
@adaptr: can you please provide examples where Iain solution won't answer the question ? –  jlliagre Dec 19 '11 at 16:56
add comment
man wc

Print newline, word, and byte counts

user@machine:~$ ls -lh datefix.vim
-rw-r--r-- 1 user     group     264 2011-06-28 23:26 datefix.vim
user@machine:~$ wc datefix.vim
  6  12 264 datefix.vim
user@machine:~$ wc datefix.vim | awk '{print $3}'
264

wc doesn't know or care about Unicode -- it thinks a byte is a byte. I also tested this out on a 234 MB .lzma file to proof it... so a simple wc -c will also get you what you want.

share|improve this answer
    
thanks, indeed. apparently i confused this with the "-m" parameter on some systems. –  harald Dec 20 '11 at 8:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.