Alex Rivera | Logout

How do you search for files containing DOS line endings (CRLF) with grep on Linux?

Asked 2008-09-16T15:50:58.910
178

I want to search for files containing DOS line endings with grep on Linux. Something like this:

grep -IUr --color '\r\n' .

The above seems to match for literal rn which is not what is desired.

The output of this will be piped through xargs into todos to convert crlf to lf like this

grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'
Edit
Report

2 Answers

237

grep probably isn't the tool you want for this. It will print a line for every matching line in every file. Unless you want to, say, run todos 10 times on a 10 line file, grep isn't the best way to go about it. Using find to run file on every file in the tree then grepping through that for "CRLF" will get you one line of output for each file which has dos style line endings:

find . -not -type d -exec file "{}" ";" | grep CRLF

will get you something like:

./1/dos1.txt: ASCII text, with CRLF line terminators
./2/dos2.txt: ASCII text, with CRLF line terminators
./dos.txt: ASCII text, with CRLF line terminators
answered 2008-09-16T16:03:28.833
3

The query was search... I have a similar issue... somebody submitted mixed line endings into the version control, so now we have a bunch of files with 0x0d 0x0d 0x0a line endings. Note that

grep -P '\x0d\x0a'

finds all lines, whereas

grep -P '\x0d\x0d\x0a'

and

grep -P '\x0d\x0d'

finds no lines so there may be something "else" going on inside grep when it comes to line ending patterns... unfortunately for me!

answered 2010-09-22T20:57:31.213

Your Answer