Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use Linux. There is a pesky ^M (Windows cariage return) somewhere hidden in thousands of configuration files, and I have to find it, because it makes the server fail.

How do I find ^M among a directories hierarchy full of configuration files?

I think I can not enter ^M on the bash command line. But I have it in a text file that I called m.txt

share|improve this question
    
Related: Remove carriage return in Unix. –  Cupcake May 15 at 21:17

5 Answers 5

up vote 31 down vote accepted
grep -r $'\r' *

Use -r for recursive search and $'' for c-style escape in Bash.

More, if you are sure it's text file, then it should be safe to run

tr -d $'\r' < filename

to remove all \r in a file. Or you can use sed to do in-place edit, so you won't need to write back

sed 's/\r//' -i filename
share|improve this answer
5  
@Nicolas: You can enter a ^M at the command line by pressing ^V^M, but it's better to use $'\r'. –  Dennis Williamson Oct 1 '10 at 5:54
    
Great, it works! Thanks for the ^V^M trick too :-) –  Nicolas Raoul Oct 1 '10 at 5:56
1  
Under Cygwin, -U is needed to make this work. And -n will tell you the line number: grep -r -U -n -e $'\r' –  Rainer Blome Jan 3 '13 at 16:16
    
Add an -l to the grep command to just view the filenames. Else you might be bombarded with matching lines. –  Brendan Byrd Mar 25 at 20:28
    
Note that using \r in sed 's/\r//' -i filename won't work with OS X's version of sed (I've read that GNU-sed will recognize it though). As a workaround, CTRL-V + CTRL-M seems to work in the place of \r instead. –  Cupcake May 15 at 21:46

When I tried, I could tell it was sort-of working, but the lines were printing blank. Add in the option:

--color=never

If you get this issue, I think it's the escape characters for color highlighting interfering with the \r character.

share|improve this answer

In regular expression style, various newlines:

Windows (CR LF)
\r\n

Unix (LF)
\n

Since the \r\n sequence is fairly unique, I think you should be able to search for it that way?

To make things worse Macs used to have just '\r' in place of newline. I cannot verify this, but I don't think MacOSX generations does that any more.

Older Macs (CR)
\r

share|improve this answer
    
In the directory that contains m.txt, grep "\r\n" * gives no result. No result either for egrep -e "\r\n" * nor grep -E "\r\n" * –  Nicolas Raoul Oct 1 '10 at 5:54
    
@nicolas ah, I misunderstood.. you meant CR only \r my bad. A full windows newline is indeed \r\n or CRLF –  Jeff Atwood Oct 1 '10 at 5:58

If you are on a Mac and use homebrew, you can do:

brew install tofrodos
fromdos file.txt

to remove all the Windows carriage returns from file.txt

To switch back to Windows carriage returns,

todos file.txt
share|improve this answer
    
to search in a folder and clean all files coming from dos, run this command : find . -type f -name "*.java" | xargs fromdos –  Taiko May 19 at 3:38

If your server does not have a bash shell, an alternative is to use the -f option on grep, in combination with your prepared m.txt file.

To check your file ...

od -c m.txt
0000000   \r
0000001

To actually do the search

grep -f m.txt *.html *.php *.asp *.whatever

or you can be a little lazy and just type *, but that means that m.txt will be included in the list that comes back.

grep -f m.txt *

The -f filename option on grep is used to specify a file that contains patterns to match, one per line. In this case there's only one pattern.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.