iqbal_cs iqbal_cs - 1 month ago 16
Linux Question

How 'tail' command returns the whole file content using parameters -c+1 or -c-1?

As the question itself, I cannot find out why

tail
acts that way.
I have a file named
myfile.txt
and its content is:

firstline
secondline
thirdline


So when I use:

tail -c-1 myfile.txt


or

tail -c+1 myfile.txt


outputs:

firstline
secondline
thirdline


man tail
:


-c, --bytes = [+] NUM

output the last NUM bytes; or use -c +NUM to output starting
with byte NUM of each file

Answer
  • tail -c+1 myfile.txt is the same as cat myfile.txt: you're telling tail to start output with the first (+1) byte (-c), in other words: the whole file.

  • tail -c-1 myfile.txt (more typically: tail -c1 myfile.txt) outputs only the last byte in myfile.txt; assuming that myfile.txt is properly terminated with a trailing \n, and is either a single-byte encoding such as ASCII or one that has single-byte ASCII encoding as a subset, such as UTF-8, this will output just that \n, i.e., a blank line.

The above applies to both the GNU (typical on Linux) and the BSD/macOS implementations of tail. If you're getting different results - as it sounds you're getting for tail -c-1 - tell us what implementation you're using / what platform you're on.