Is there a way to quickly identify files with Windows or Unix line termination?

I know we can use dos2unix to convert between Windows and Unix line termination. I am wondering if there is any command that can tell me if a file has Windows or Unix line termination?


$ file f1 f2 f3
f1: ASCII text, with CRLF, LF line terminators
f2: ASCII text, with CRLF line terminators
f3: ASCII text

If you feel it necessary to check every line in the file,you can do this:

$ grep -c "^M" f1 f2
f1:0
f2:3

$ wc -l f1 f2
 3 f1
 3 f2
 6 total

The "^M" was entered using Ctrl+V Ctrl+M and is the ASCII carriage-return (CR) character.

Here we see that file f1 has three lines but no CRs so all line endings must be Unix style solo LFs.

File f2 has equal numbers of lines and CRs so it is reasonable to guess that it uses the CR,LF line-endings as used by MSDOS and Windows.


On Windows, a quick way to tell is to open your file in Notepad. Notepad will show line-breaks only on windows style terminations (CR+LF), and not unix terminations (LF). So your unix text will look like this:

Line1Line2Line3Line4

whereas, windows text will look like this:

line1
line2
line3
line4

I'm not much familiar with unix/linux platform, but I'm sure you can use similar hacks with programs like gedit or emacs.