Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a quick way to detect the EOL for a file?

I normally just open up the file in Vim and check out the status line. I'm sure there's a better and quicker way.

I figured a grep might work, but I am not sure if it's the most elegant way to detect the EOLs. I'm specifically focused on detecting the Mac EOL (CR), Windows (CRLF) and Unix (LF).

A a *nix/Bash solution is preferred, but I am interested in other platforms as well.

(Earlier, I answered a Server Fault question that could possibly be an EOL issue, and it got me wondering if there was a quick way.)

share|improve this question
I guess you could get it with (\r|\n|\r\n), but like you say, that's not very elegant. – Phoshi Oct 24 '10 at 22:07
What are the 3 possible EOLs? – barlop Oct 24 '10 at 22:12
Mac (CR), Windows(CRLF) and Linux (LF). I'm sure there's more out there but those are the EOL's I care about. I'll modify the question. – Belmin Fernandez Oct 24 '10 at 22:17
up vote 10 down vote accepted

file (at least with the magic database commonly found on Linux) is an easy way. It won't warn you about mixed-ending files, though.

$ file unix mac dos  # the files have line endings matching their names
unix: ASCII text
mac:  ASCII text, with CR line terminators
dos:  ASCII text, with CRLF line terminators
share|improve this answer
Find this one hard to beat. – Belmin Fernandez Oct 24 '10 at 23:22
'file' is not a reliable way, at least not without extra parameters for 'file'. On an XML file, it will only output "XML document text", no matter the EOL character sequence (observed on MinGW 1.0.17, 2011-05-30). (I ran into this problem at work.) – Peter Mortensen May 1 '12 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .