When I give wc command on a file with contents given below, it gives the number of characters as 30. Does it include the end of file character? Since including the space and newline there are only 29 characters.

Hello World
Again Hello World

The output is

2  5 30 test

There is no end-of-file character in Unix or Linux filesystems. The read() system call returns 0 on end-of-file condition, if the file descriptor in use refers to a regular file. read() works differently on sockets and pipes. You don't get a special character to mark end of file.

wc gave you 30 as a character or byte count because the first line has 12 characters counting end-of-line (ASCII line feed, 0x0a numerically), and the second line has 18, also counting the newline (a.k.a. line feed).

You can double-check the character count in this case with ls -l, and if you've got hexdump or xxd you can get a hexadecimal printout showing you the 0x0a valued newlines.

The C standard library function fgetc() does return -1 on end-of-file, but that's done in the library code, not by Unix (or Linux) or the read() system call.

  • I didnt put line feed after the second line. But od command shows new line nl at the end. Why is it so? – user3539 Feb 28 '13 at 2:49
  • 3
    @user3539 - It's traditional/convention for Unix/Linux/BSD text editors to put a newline/line feed at the end of the last line in the file, even if you don't. If you made the file with vi/vim, ex or ed, it did it for you. I don't know about all these modern, GUI editors, but I presume they do it too. – Bruce Ediger Feb 28 '13 at 3:12
  • @user3539 Yes, you did. All the lines in a text file end with a newline, including the last one. See What's the point in adding a new line to the end of a file? – Gilles 'SO- stop being evil' Feb 28 '13 at 14:17
  • Gilles: While it is common that all lines in a text file end with a new line, it is not required. Unix has no special "text" file format. All ordinary files are just unstructured (by the filesystem) sequences of bytes, which may or may not end in a newline. For example, on linux or OS-X, "echo -n foo > foo.txt" will create a file containing the three 7-bit ascii chars and no terminating newline. – Tim B Mar 1 '13 at 14:21

It is the number of bytes in the file. See the man page.

Here is an example with 5 bytes:

$ echo 1234 > foo.txt
$ od -ta foo.txt
0000000   1   2   3   4  nl
$ ls -l foo.txt
-rw-r--r-- 1 tim None 5 Feb 27 21:26 foo.txt
$ wc foo.txt
1 1 5 foo.txt

Now add a blank line:

$ echo >> foo.txt
$ ls -l foo.txt
-rw-r--r-- 1 tim None 6 Feb 27 21:29 foo.txt
$ wc foo.txt
2 1 6 foo.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.