Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How can I tell if a file has CRLF?

use a text editor like notepad++ that can help you with understanding the line ends. It will show you the line end formats used as either Unix(LF) or Macintosh(CR) or Windows(CR LF) on the task bar of the tool. you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.

Can you use CRLF in Linux?

The characters CRLF are often used to represent the carriage return and linefeed sequence that ends lines on Windows text files. Those who like to gaze at octal dumps will spot the \r \n. Linux text files, by comparison, end with just linefeeds.


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

Use Ctrl+V, Ctrl+M to enter a literal Carriage Return character into your grep string. So:

grep -IUr --color "^M"

will work - if the ^M there is a literal CR that you input as I suggested.

If you want the list of files, you want to add the -l option as well.

Explanation

  • -I ignore binary files
  • -U prevents grep from stripping CR characters. By default it does this it if it decides it's a text file.
  • -r read all files under each directory recursively.

Using RipGrep (depending on your shell, you might need to quote the last argument):

rg -l \r
-l, --files-with-matches
Only print the paths with at least one match.

https://github.com/BurntSushi/ripgrep


If your version of grep supports -P (--perl-regexp) option, then

grep -lUP '\r$'

could be used.