Get actual file size with du on Linux
You can use
du, to report on the size of directories or files, but when my file is smaller than the block size I don't see the output I expect.
With a small file, this should be the size of the number of characters.
$ echo 'Every Good Boy Deserves Fudge' > staff.txt $ cat staff.txt | wc -c 30
But when I use
du to examine file, I don't see 30.
$ du -h staff.txt 4.0K staff.txt
du measures in block sizes because in general if any part of a block is used, then for the purposes of the operating system the entire block is used.
You can tell
du to care only about the size of the file with
--apparent-size which is only apparent because between the beginning and end of the file the OS can't tell which bytes are in use or are not in use.
$ du --apparent-size staff.txt 1 staff.txt
When reporting apparent size it rounds up to kilobytes, or
To get the actual size of the file, you can use
-b which is the same as
$ du -b staff.txt 30 staff.txt