Issue
I would like to find out which of my files in a directory are dos text files (as opposed to unix text files). Specifically, these are files that, when opened in Vim, the bottom bar will say something like "filename.php" [dos] [noeol]
I've tried find . -name "*.php" | xargs grep ^M -l
, but I don't get reliable results. In particular, it doesn't detect certain dos files that don't have ^M
characters in them.
Is there a better alternative?
Solution
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.
Answered By - paxdiablo Answer Checked By - Terry (WPSolving Volunteer)