How to determine the line ending of a file

ScriptingLine Endings

Scripting Problem Overview


I have a bunch (hundreds) of files that are supposed to have Unix line endings. I strongly suspect that some of them have Windows line endings, and I want to programmatically figure out which ones do.

I know I can just run

flip -u
or something similar in a script to convert everything, but I want to be able to identify those files that need changing first.

Scripting Solutions


Solution 1 - Scripting

You can use the http://en.wikipedia.org/wiki/File_(Unix)">`file`</a> tool, which will tell you the type of line ending. Or, you could just use dos2unix -U which will convert everything to Unix line endings, regardless of what it started with.

Solution 2 - Scripting

You could use grep

egrep -l $'\r'\$ *

Solution 3 - Scripting

Something along the lines of:

perl -p -e 's[\r\n][WIN\n]; s[(?<!WIN)\n][UNIX\n]; s[\r][MAC\n];' FILENAME

though some of that regexp may need refining and tidying up.

That'll output your file with WIN, MAC, or UNIX at the end of each line. Good if your file is somehow a dreadful mess (or a diff) and has mixed endings.

Solution 4 - Scripting

Here's the most failsafe answer. Stimms answer doesn account for subdirectories and binary files

find . -type f -exec file {} \; | grep "CRLF" | awk -F ':' '{ print $1 }'
  • Use file to find file type. Those with CRLF have windows return characters. The output of file is delimited by a :, and the first field is the path of the file.

Solution 5 - Scripting

Unix uses one byte, 0x0A (LineFeed), while windows uses two bytes, 0x0D 0x0A (Carriage Return, Line feed).

If you never see a 0x0D, then it's very likely Unix. If you see 0x0D 0x0A pairs then it's very likely MSDOS.

Solution 6 - Scripting

Windows use char 13 & 10 for line ending, unix only one of them ( i don't rememeber which one ). So you can replace char 13 & 10 for char 13 or 10 ( the one, which use unix ).

Solution 7 - Scripting

When you know which files has Windows line endings (0x0D 0x0A or \r \n), what you will do with that files? I supose, you will convert them into Unix line ends (0x0A or \n). You can convert file with Windows line endings into Unix line endings with sed utility, just use command:

$> sed -i 's/\r//' my_file_with_win_line_endings.txt

You can put it into script like this:

#!/bin/bash

function travers()
{
    for file in $(ls); do
        if [ -f "${file}" ]; then
            sed -i 's/\r//' "${file}"
        elif [ -d "${file}" ]; then
            cd "${file}"
            travers
            cd ..
        fi
    done
}

travers

If you run it from your root dir with files, at end you will be sure all files are with Unix line endings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnwahmaetView Question on Stackoverflow
Solution 1 - ScriptingAdam RosenfieldView Answer on Stackoverflow
Solution 2 - ScriptingstimmsView Answer on Stackoverflow
Solution 3 - ScriptingjoachimView Answer on Stackoverflow
Solution 4 - ScriptingBryce GuintaView Answer on Stackoverflow
Solution 5 - ScriptingAdam DavisView Answer on Stackoverflow
Solution 6 - ScriptingTcKsView Answer on Stackoverflow
Solution 7 - Scripting1ac0View Answer on Stackoverflow