Finding files without New Lines at the End

DateReadtime 1 minutes Tags


Not having newlines at the ends of files messes up lots of command line operations such as wc, >>, git and cat.

POSIX definition of a line:

3.206 Line A sequence of zero or more non- <newline> characters plus a terminating <newline> character.


Example 1:

Given two files 1.txt and 2.txt without newlines at the end:

$ cat 1.txt 2.txt

The same two files with newlines:

$ cat 1.txt 2.txt

Example 2:

Appending to the end of a file doesn't work properly without a newline:

$ cat .gitignore
$ echo "*~" >> .gitignore
$ cat .gitignore

Example 3:

Word count (wc) doesn't correctly count lines:

$ cat missing.txt | wc -l


Finding missing newlines

Recursively find files missing trailing newlines in directory:

find . -type f -print0 | xargs -0 -L1 bash -c 'test "$(tail -c 1 "$0")" && echo "No new line at end of $0"'

Find files missing trailing newlines in a git repo:

git ls-files -z | xargs -0 -L1 bash -c 'test "$(tail -c 1 "$0")" && echo "No new line at end of $0"'

Adding missing newlines

  • Using your editor of choice, go to the last line and add newline

  • Ed:

    ed -s file <<< w
  • Sed:

    sed -i -e '$a\' file