Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

13
  • 46
    wc -c <"$FILENAME" gives the size with no other cruft, thus size=$(wc -c <"$FILENAME"). Commented Jul 14, 2011 at 9:58
  • 6
    Just one more point: I just tested it and wc -c < file seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified. Commented Apr 4, 2016 at 16:29
  • 5
    @EdwardFalk: GNU wc -c uses fstat, but then seeks to second-last block of the file and reads the last up-to st_blksize bytes. Apparently this is because files in Linux's /proc and /sys for example have stat sizes that are only approximate, and wc wants to report the actual size, not the stat-reported size. I guess it would be weird for wc -c to report a different size than wc, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage... Commented Apr 12, 2017 at 5:28
  • 1
    It seems like printf still sees the indentation, e.g. printf "Size: $size" -> size: <4 spaces> 54339. On the other hand echo ignores the whitespace. Any way to make it consistent? Commented May 2, 2017 at 12:43
  • 2
    @keithpjolley: By calling fstat. Try running strace wc -c </etc/passwd and you can see what it is doing. Commented Jan 8, 2019 at 18:19