Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using cygwin's grep to find all instances of hardcoded paths on c drive. doing:

grep -r "c:\\" .

gives grep: trailing backslash

Does anybody know how I can do this?

share|improve this question
    
Just for a chance, try grep -r "c:[\]" . –  Serge Oct 2 '12 at 19:19
    
Thanks for response! it gives no error but it doesn't match anything... –  noobler Oct 2 '12 at 19:24
    
EDIT - scratch that. Sorry. That does work too - the lack of matching was my error. Thanks @Serge –  noobler Oct 2 '12 at 19:40
add comment

2 Answers 2

up vote 10 down vote accepted

Your error is because you are using double quotes ("), which allow the contents to be interpreted by the shell before it gets to grep.

Try grep -r 'c:\\' . instead.

echo 'c:\' > test
ire@localhost$ cat test
c:\
ire@localhost$ grep -r 'c:\\' test
c:\

Explanation: \ has a special meaning, both to the shell and to grep. It's used as an escape character, to allow the next character to be interpreted literally.

When you do grep "c:\\", the shell picks up the content, converts it to the literal string c:\, and passes that to grep. grep sees the single backslash, and interprets it as an escape character. But there's no character following the \ to escape! So, quite reasonably it complains.

Using single quotes (') protects the content from the shell. But you still need two slashes, because you need to tell grep this is a literal backslash you are wanting to search for.

Alternatively, you could have done:

grep -rF 'C:\' .

or

grep -rF "C:\\" .

The -F option to grep (formerly known as the fgrep command) tells grep to look for fixed strings, and therefore there's nothing to escape and the backslash is not special to grep (but still is for the shell inside double quotes).

share|improve this answer
4  
Alternatively, do grep -r "c:\\\\" so grep will see c:\\ as its input string (single quotes are a better method) –  Michael Mrozek Oct 2 '12 at 19:32
    
it works! thanks! –  noobler Oct 2 '12 at 19:36
    
grep -F (fgrep) seems the most "fitting" choice for this particular case, since `C:` is intended as a literal string. –  jw013 Oct 3 '12 at 15:19
add comment

Or just use "c:/" instead (which also works under Windows).

share|improve this answer
add comment

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.