In Linux, why does an empty file have a size of 0, but a text file with any content has a size the length of the content + 1?
5,347
Solution 1
An extra byte is for the line end at the end of the file, it's quite common for Linux text editors to add this line end after the last line.
Solution 2
Probably a trailing new-line character. For example, a file created in a text editor containing only an 'a' may actually contain 2 bytes:
$ cat /tmp/test_text | hexdump -C
00000000 61 0a |a.|
00000002
However, using echo -n
(no new line) gives us a size of 1 byte:
$ echo -n 'a' > /tmp/test_text
$ ls -l /tmp/test_text
-rw-r--r-- 1 redacted redacted 1 1 Sep 21:09 /tmp/test_text
$ cat /tmp/test_text | hexdump -C
00000000 61 |a|
00000001
Related videos on Youtube
Author by
Kevin Rood
Updated on September 18, 2022Comments
-
Kevin Rood over 1 year
This may be a *nix thing, I'm not sure.
-
Dana the Sane over 12 yearsThe gcc compiler even complains if this blank line is not there.
-
Will Dixon over 12 yearsSome text editors (e.g.
vim
) will also warn you if you don't have the EOL.