[ACCEPTED]-How to determine the line ending of a file-line-endings

Accepted answer
Score: 72

You can use the file tool, which will tell you 4 the type of line ending. Or, you could 3 just use dos2unix -U which will convert everything 2 to Unix line endings, regardless of what 1 it started with.

Score: 29

You could use grep

egrep -l $'\r'\$ *


Score: 14

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 5 of that regexp may need refining and tidying 4 up.

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

Score: 5

Here's the most failsafe answer. Stimms 2 answer doesn account for subdirectories 1 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.
Score: 2

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

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

Score: 0

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

Score: 0

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

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

You 4 can put it into script like this:


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


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

More Related questions