How to find out line-endings in a text file?

Marco Ceppi picture Marco Ceppi · Aug 25, 2010 · Viewed 332.6k times · Source

I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.

  • Are there any switches within vi, less, more, etc?

  • In addition to seeing the line-endings, I need to know what type of line end it is (CRLF or LF). How do I find that out?

Answer

You can use the file utility to give you an indication of the type of line endings.

Unix:

$ file testfile1.txt
testfile.txt: ASCII text

"DOS":

$ file testfile2.txt
testfile2.txt: ASCII text, with CRLF line terminators

To convert from "DOS" to Unix:

$ dos2unix testfile2.txt

To convert from Unix to "DOS":

$ unix2dos testfile1.txt

Converting an already converted file has no effect so it's safe to run blindly (i.e. without testing the format first) although the usual disclaimers apply, as always.