Linux - check if there is an empty line at the end of a file

Black picture Black · Jan 22, 2016 · Viewed 16.9k times · Source

NOTE: this question used to be worded differently, using “with/out newline” instead of “with/out empty line”

I have two files, one with an empty line and one without:

File: text_without_empty_line

$root@kali:/home#cat text_without_empty_line
This is a Testfile
This file does not contain an empty line at the end
$root@kali:/home#

File: text_with_empty_line

$root@kali:/home#cat text_with_empty_line
This is a Testfile
This file does contain an empty line at the end

$root@kali:/home#

Is there a command or function to check if a file has an empty line at the end? I already found this solution, but it does not work for me. (EDIT: IGNORE: A solution with preg_match and PHP would be fine as well.)

Answer

Camila Masetti picture Camila Masetti · Nov 15, 2017

Just type:

cat -e nameofyourfile

If there is a newline it will end with $ symbol. If not, it will end with a % symbol.