0

I'm trying to write a shell script in busybox to check the filesize of a file. Having read that stat is more reliable then ls, I decided to use that, but somehow when using the following command:

stat -c %s filename

I get the following output: 559795. This goes for the following 2 files (shown using ls -la):

0 Jan 20 16:32 foo_empty
4 Jan 20 16:32 foo_not_empty

Anyone know what's happening there? I can just go back to using ls, but I'm not understanding what's happening here, and that's bothering me..

datadevil
  • 535
  • 1
  • 7
  • 22
  • 1
    cam you look at the full output of stat with `stat foo_empty` and verify that the 559795 number appears in the size field and not some other field? – stew Jan 20 '12 at 16:40
  • File: "foo_not_empty" Size: 559795 Blocks: 559795 IO Block: 4096 regular file Device: 98a7bh/559795d Inode: 559795 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Access: Fri Jan 20 16:32:29 2012 Modify: Mon Jan 23 10:41:06 2012 Change: Mon Jan 23 10:41:06 2012 – datadevil Jan 23 '12 at 09:43

4 Answers4

1

if stat is reporting the same odd number for filesize, number of blocks, inode, and device. I'd suspect filesystem corruption. You might try fsck and see if it finds/fixes problems.

stew
  • 9,388
  • 1
  • 30
  • 43
0

In my case, stat is missing from the busybox (embedded system). For me, performance isn't an issue, so I ended up with file_size="$(wc -c ./this-file)".

Superlexx
  • 41
  • 2
0

Works for me:

ls -l foo_*
-rw------- 1 fooo users    0 Jan 20 10:30 foo_empty
-rw------- 1 fooo users 3767 Jan 20 10:30 foo_not_empty

busybox stat -c %s foo_empty; busybox stat -c %s foo_not_empty 
0
3767

Are you using an old version of busybox, possibly on a 64 bit OS? I'm using

BusyBox v1.19.3 (2012-01-03 13:39:53 PST) multi-call binary.
Mark Wagner
  • 18,019
  • 2
  • 32
  • 47
  • a lot older: BusyBox v1.1.1 (2009.12.24-08:39+0000) multi-call binary. Made the non-empty file a lot bigger, assuming that it was some kind of default filesize, but that does not make a difference. – datadevil Jan 23 '12 at 09:39
-1

You can also use du to check file size. du -h will provide you with a "human readable" output.

Tim
  • 3,017
  • 17
  • 15