This question and this question point out that generally the standard is to end text files with newline characters to meet the POSIX standard, and this is something expected with GNU tools and other POSIX compliant tools.

However, as a C# developer, I know that tools that work with csproj files generally don't include a terminating newline character. I've also seen tools that remove the terminating newline from json and xml files. I think it makes sense to avoid including a newline at the end of text files if the common tools that work with the files would just end up removing them again.

So, what are some types of text files that shouldn't end with newline characters due to tooling?

  • 5
    A file that don't end with a newline is simply not a text file in the POSIX sense. It's a stream of bytes. Tools that can work with a stream of bytes should be able to use these. Refer to the tool's manual.
    – Kusalananda
    Mar 21 '20 at 23:14
  • 2
    Programs that remove trailing newlines from textfiles seem to be broken. I would avoid such programs.
    – schily
    Mar 22 '20 at 13:02

Though not exactly a text file, a symbolic link is effectively a tiny file containing a piece of text (often fitting into the inode itself, so no data blocks need to be allocated to the file).

A symbolic link can end with a newline, but under the most common circumstances it shouldn't.

Another type of file that should not (well, cannot) end with a newline is the empty file.

In UNIX, a text file is either empty, or contains one or more possibly empty lines, each of which is terminated by a newline. If the last line isn't terminated by a newline, the file isn't a proper text file.

The empty file contains no bytes at all; the moment you add a newline, you have changed its "type" from empty to non-empty.


a text file used as input for search terms processed by grep -f must not end with newline (** in text editor) otherwise that would match everything

you can try for yourself on this example
grep not finding all instances of gene


Edit: please note question is about collecting examples where "text files shouldn't end with newline characters due to tooling." Although my first statement does not apply in general it is a perfectly example for such "type of text file" (text editor created file) and therefore valid in context of question, because that is what everyone experience when using a text editor.

It doesn't meant as broad statement regarding \n newline character on file system level but as example for corner case from a normal user's perspective

thx @Kusalananda @ilkkachu for clarifying in real that is two \n\n newline characters (although one is silently added by text editor and therefore invisible)

  • 1
    no, that's wrong. First of all, the pattern file can contain multiple patterns, on distinct lines, so it must be able to deal with newlines. Second, the standard actually demands that they are terminated with newlines: "-f pattern_file Read one or more patterns from the file named by the pathname pattern_file. Patterns in pattern_file shall be terminated by a <newline>." And, umm, I'm not sure how the linked question shows that newlines don't work?
    – ilkkachu
    Jan 18 '21 at 0:53
  • @ilkkachu please try it. i can post a screenshot if you don't believe it matters. yes the file contain multiple patterns, and if the file is terminated (or contain) any newlines ^$ grep will treat it as zero length pattern which will match every line
    – alecxs
    Jan 18 '21 at 4:05
  • 1
    A newline is not the same as an empty line. You get an empty line if you have two consecutive newline characters (or if the file starts with a newline character).
    – Kusalananda
    Jan 18 '21 at 5:42
  • @Kusalananda thx will check this again i just copy&pasted example in some text file, it might happend i accidentially terminated with \n\n
    – alecxs
    Jan 18 '21 at 7:34
  • @alecxs, I did try. printf '15\n^7[12]\n' > patterns.txt; seq 100 | grep -f patterns.txt prints 15, 71, 72. If you add another newline, creating an empty line, then it matches all input lines, like Kusalananda said. FWIW, GNU grep doesn't care if the pattern file is missing a newline.
    – ilkkachu
    Jan 18 '21 at 12:33

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.