How to find dos format files in a linux file system

i had good luck with

find . -name "*.php" -exec grep -Pl "\r" {} \;

How about:

find . -name "*.php" | xargs file | grep "CRLF"

I don't think it is reliable to try and use ^M to try and find the files.


Not sure what you mean exactly by "not reliable" but you may want to try:

find . -name '*.php' -print0 | xargs -0 grep -l '^M$'

This uses the more atrocious-filenames-with-spaces-in-them-friendly options and only finds carriage returns immediately before the end of line.

Keep in mind that the ^M is a single CTRLM character, not two characters.

And also that it'll list files where even one line is in DOS mode, which is probably what you want anyway since those would have been UNIX files mangled by a non-UNIX editor.


Based on your update that vim is reporting your files as DOS format:

If vim is reporting it as DOS format, then every line ends with CRLF. That's the way vim works. If even one line doesn't have CR, then it's considered UNIX format and the ^M characters are visible in the buffer. If it's all DOS format, the ^M characters are not displayed:

Vim will look for both dos and unix line endings, but Vim has a built-in preference for the unix format.

- If all lines in the file end with CRLF, the dos file format will be applied, meaning that each CRLF is removed when reading the lines into a buffer, and the buffer 'ff' option will be dos.
- If one or more lines end with LF only, the unix file format will be applied, meaning that each LF is removed (but each CR will be present in the buffer, and will display as ^M), and the buffer 'ff' option will be unix.

If you really want to know what's in the file, don't rely on a too-smart tool like vim :-)

Use:

od -xcb input_file_name | less

and check the line endings yourself.