53

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.
nwahmaet
  • 3,589
  • 3
  • 28
  • 35

7 Answers7

72

You can use the file 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.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 5
    file doesn't show line ending. Ex. : "file .bashrc => .bashrc: ASCII English text" Need some extra keys ? – Fedir RYKHTIK Feb 09 '12 at 10:32
  • 10
    @Fedir: Yes, it does, it's just that if the file has regular LF line endings, then it won't print any output. But if the file has CRLF, bare CR, or mixed line endings, it will tell you that. – Adam Rosenfield Feb 09 '12 at 21:55
  • 2
    Didn't work for me on a CRLF-only Perl script on OS X. Might be a GNU extension? – Tim Yates Jun 11 '12 at 20:34
  • 3
    This works on some file types but not others. On Linux, it doesn't report the line endings for html files for example. – Edward Anderson Apr 09 '13 at 04:48
  • "file foo.txt" worked fine on OS X 10.9. It printed "foo.txt: ASCII text, with CRLF line terminators" – Bogdan Calmac Mar 14 '14 at 17:19
  • Almost 10 years later, it's certainly worth mentioning that `dos2unix` features the `-i` option to get the information on the file. I have found this to be the most reliable way to identify the culprits. This is also described in https://unix.stackexchange.com/a/231408/17800 – ikaerom Nov 04 '17 at 23:16
29

You could use grep

egrep -l $'\r'\$ *
Edward Anderson
  • 13,591
  • 4
  • 52
  • 48
stimms
  • 42,945
  • 30
  • 96
  • 149
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 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.

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
joachim
  • 28,554
  • 13
  • 41
  • 44
5

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.
Ulf Lindback
  • 13,974
  • 3
  • 40
  • 31
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
  • Indeed the most failsafe way. To convert only all found files just run `find . -type f -exec file {} \; | grep "CRLF" | awk -F ':' '{ print $1 }' | xargs flip -ub` afterwards. – pixelbrackets Jan 24 '17 at 09:36
  • 3
    Most failsafe it is not -- `file` does not always even tell "CRLF" in its output, that depends on what kind of file it is. I have discovered that for SVG files -- containing text much like plaintext files -- `file` does not mention the kind of line ending used. This script thus is not file type agnostic. Just saying. Otherwise looks like a sane one-liner, aforementioned limitation non-withstanding. – Armen Michaeli Apr 16 '17 at 12:51
2

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.

Hugo
  • 27,885
  • 8
  • 82
  • 98
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
0

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 ).

TcKs
  • 25,849
  • 11
  • 66
  • 104
0

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.

1ac0
  • 2,875
  • 3
  • 33
  • 47