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..

2
  • 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, 2012 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, 2012 at 9:43

4 Answers 4

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.

2
  • This is over multiple disks, so that'd be very weird...
    – datadevil
    Jan 24, 2012 at 8:27
  • 2
    not as weird as having 1 byte blocks and inode number that happens to exactly match the number of bytes
    – stew
    Jan 24, 2012 at 14:23
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.
1
  • 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, 2012 at 9:39
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)".

-1

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

1
  • This is a good idea, but is not really answering the OP's question.
    – EEAA
    Jan 20, 2012 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .