22

How should I grep for a string containing a forward slash like ./.?

TRiG
  • 10,148
  • 7
  • 57
  • 107
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
  • 9
    Can you maybe accept an answer to mark the question as resolved? – MERose Nov 05 '15 at 14:30
  • On Windows (Git for Windows, MinGW, etc.), forward slashes are sometimes affected by path conversion. Prefix the grep command with `MSYS_NO_PATHCONV=1` if needed. – Joey Adams Dec 12 '22 at 17:54

3 Answers3

15

The forward slash is not a special character in grep, but may be in tools like sed, Ruby, or Perl. You probably want to escape your literal periods, though, and it does no harm to escape the slash. This should work in all cases:

\.\/\.
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
13

You'll just need to escape the periods with a backslash. So if I have a file foo.txt with contents:

./.
foo
bar
./.

I can run grep \./\. test.txt, which should just print the two ./. lines.

Peter
  • 1,349
  • 11
  • 21
0

Try fgrep. In your case fgrep '/<string>'

Mahaveer Jangir
  • 597
  • 7
  • 15