How to fix “No newline at end of file” warning for lots of files?

前端 未结 11 1843
抹茶落季
抹茶落季 2020-12-03 01:48

I have a huge number of source files that are all lacking a newline at the end.

How do I automatically add a newline to the end of each of them?

Some may alr

11条回答
  •  囚心锁ツ
    2020-12-03 02:28

    Converted Norman's answer to a split one-liner for convenience.

    for i in * ; do  echo $i; \
     if diff /dev/null "$i" | tail -1 | \
      grep '^\\ No newline' > /dev/null; then echo >> "$i"; \
     fi; done
    

    Replace * with whatever file pattern you want, eg *.c

    And another to just tell you which files are broken:

    for i in * ; do \
     if diff /dev/null "$i" | tail -1 | \
      grep '^\\ No newline' > /dev/null; then  echo $i; \
     fi; done
    

提交回复
热议问题