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

13,813

Solution 1

$ 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 MS-DOS and Windows.

Solution 2

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.

Share:
13,813

Related videos on Youtube

Raistlin
Author by

Raistlin

Updated on September 18, 2022

Comments

  • Raistlin
    Raistlin over 1 year

    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?

  • miracle2k
    miracle2k almost 10 years
    file doesn't output line ending status if it can find a closer match than ASCII, i.e. setup.py: a python script text executable. A can't seem to find a flag to change that behaviour.
  • phuclv
    phuclv about 2 years
    this is no longer true. Notepad has supported CR/LF/CRLF files properly since 2018 so those files won't be displayed as a single line