<p>I want to search for files containing DOS line endings with grep on Linux.  Something like this:</p>
<pre class="lang-sh prettyprint-override"><code>grep -IUr --color '\r\n' .
</code></pre>
<p>The above seems to match for literal <code>rn</code> which is not what is desired.</p>
<p>The output of this will be piped through xargs into todos to convert crlf to lf like this</p>
<pre><code>grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'
</code></pre>

[{'answer_id': 73886, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 7, 'selected': False, 'text': "<p>Use <kbd>Ctrl</kbd>+<kbd>V</kbd>, <kbd>Ctrl</kbd>+<kbd>M</kbd> to enter a literal Carriage Return character into your grep string. So:</p>\n<pre><code>grep -IUr --color &quot;^M&quot;\n</code></pre>\n<p>will work - if the <code>^M</code> there is a literal CR that you input as I suggested.</p>\n<p>If you want the list of files, you want to add the <code>-l</code> option as well.</p>\n<p><strong>Explanation</strong></p>\n<ul>\n<li><code>-I</code> ignore binary files</li>\n<li><code>-U</code> prevents grep from stripping CR characters. By default it does this it if it decides it's a text file.</li>\n<li><code>-r</code> read all files under each directory recursively.</li>\n</ul>\n"}, {'answer_id': 73969, 'author': 'Thomee', 'author_id': 12825, 'author_profile': 'https://Stackoverflow.com/users/12825', 'pm_score': 9, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code>find . -not -type d -exec file "{}" ";" | grep CRLF</code></pre>\n\n<p>will get you something like:</p>\n\n<pre><code>./1/dos1.txt: ASCII text, with CRLF line terminators\n./2/dos2.txt: ASCII text, with CRLF line terminators\n./dos.txt: ASCII text, with CRLF line terminators</code></pre>\n'}, {'answer_id': 74739, 'author': 'Linulin', 'author_id': 12481, 'author_profile': 'https://Stackoverflow.com/users/12481', 'pm_score': 4, 'selected': False, 'text': "<p>If your version of grep supports <strong>-P (--perl-regexp)</strong> option, then</p>\n\n<pre><code>grep -lUP '\\r$'\n</code></pre>\n\n<p>could be used.</p>\n"}, {'answer_id': 3184434, 'author': 'yabt', 'author_id': 384287, 'author_profile': 'https://Stackoverflow.com/users/384287', 'pm_score': 4, 'selected': False, 'text': '<pre><code># list files containing dos line endings (CRLF)\n\ncr="$(printf "\\r")"    # alternative to ctrl-V ctrl-M\n\ngrep -Ilsr "${cr}$" . \n\ngrep -Ilsr $\'\\r$\' .   # yet another &amp; even shorter alternative\n</code></pre>\n'}, {'answer_id': 3773573, 'author': 'Peter Y', 'author_id': 455551, 'author_profile': 'https://Stackoverflow.com/users/455551', 'pm_score': 2, 'selected': False, 'text': '<p>The query was search... I have a similar issue... somebody submitted mixed line\nendings into the version control, so now we have a bunch of files with <code>0x0d</code>\n<code>0x0d</code> <code>0x0a</code> line endings. Note that</p>\n\n<pre><code>grep -P \'\\x0d\\x0a\'\n</code></pre>\n\n<p>finds all lines, whereas</p>\n\n<pre><code>grep -P \'\\x0d\\x0d\\x0a\'\n</code></pre>\n\n<p>and</p>\n\n<pre><code>grep -P \'\\x0d\\x0d\'\n</code></pre>\n\n<p>finds no lines so there may be something "else" going on inside grep\nwhen it comes to line ending patterns... unfortunately for me!</p>\n'}, {'answer_id': 7715813, 'author': 'MykennaC', 'author_id': 30818, 'author_profile': 'https://Stackoverflow.com/users/30818', 'pm_score': 1, 'selected': False, 'text': "<p>If, like me, your minimalist unix doesn't include niceties like the <strong>file</strong> command, and backslashes in your <strong>grep</strong> expressions just don't cooperate, try this:</p>\n\n<pre><code>$ for file in `find . -type f` ; do\n&gt; dump $file | cut -c9-50 | egrep -m1 -q ' 0d| 0d'\n&gt; if [ $? -eq 0 ] ; then echo $file ; fi\n&gt; done\n</code></pre>\n\n<p>Modifications you may want to make to the above include:</p>\n\n<ul>\n<li>tweak the <strong>find</strong> command to locate only the files you want to scan</li>\n<li>change the <strong>dump</strong> command to <strong>od</strong> or whatever file dump utility you have</li>\n<li>confirm that the <strong>cut</strong> command includes both a leading and trailing space as well as just the hexadecimal character output from the <strong>dump</strong> utility</li>\n<li>limit the <strong>dump</strong> output to the first 1000 characters or so for efficiency</li>\n</ul>\n\n<p>For example, something like this may work for you using <strong>od</strong> instead of <strong>dump</strong>:</p>\n\n<pre><code> od -t x2 -N 1000 $file | cut -c8- | egrep -m1 -q ' 0d| 0d|0d$'\n</code></pre>\n"}, {'answer_id': 13643222, 'author': 'Zombo', 'author_id': 1002260, 'author_profile': 'https://Stackoverflow.com/users/1002260', 'pm_score': 6, 'selected': False, 'text': '<p>Using RipGrep (depending on your shell, you might need to quote the last argument):</p>\n<pre><code>rg -l \\r\n</code></pre>\n<pre><code>-l, --files-with-matches\nOnly print the paths with at least one match.\n</code></pre>\n<p><a href="https://github.com/BurntSushi/ripgrep" rel="nofollow noreferrer">https://github.com/BurntSushi/ripgrep</a></p>\n'}, {'answer_id': 47165349, 'author': 'Murali Krishna Parimi', 'author_id': 5943385, 'author_profile': 'https://Stackoverflow.com/users/5943385', 'pm_score': 2, 'selected': False, 'text': '<p>You can use file command in unix. It gives you the character encoding of the file along with line terminators.</p>\n\n<pre><code>$ file myfile\nmyfile: ISO-8859 text, with CRLF line terminators\n$ file myfile | grep -ow CRLF\nCRLF  \n</code></pre>\n'}, {'answer_id': 61631159, 'author': 'dessert', 'author_id': 6164712, 'author_profile': 'https://Stackoverflow.com/users/6164712', 'pm_score': 2, 'selected': False, 'text': '<p><code>dos2unix</code> has a file information option which can be used to show the files that would be converted:</p>\n<pre class="lang-sh prettyprint-override"><code>dos2unix -ic /path/to/file\n</code></pre>\n<p>To do that recursively you can use <code>bash</code>’s <code>globstar</code> option, which for the current shell is enabled with <code>shopt -s globstar</code>:</p>\n<pre class="lang-sh prettyprint-override"><code>dos2unix -ic **      # all files recursively\ndos2unix -ic **/file # files called “file” recursively\n</code></pre>\n<p>Alternatively you can use <code>find</code> for that:</p>\n<pre class="lang-sh prettyprint-override"><code>find -type f -exec dos2unix -ic {} +            # all files recursively (ignoring directories)\nfind -name file -exec dos2unix -ic {} + # files called “file” recursively\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73833', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10245/']