0

I thought I found the perfect answer with How do I find all files containing specific text on Linux?, so I tried it:

[Michael@devserver ~]$ grep -rnw '/var/www/concrete5.7.5.9/' -e "Concrete\Core\Block\BlockController"
[Michael@devserver ~]$

None show, but I know there should have been a match.

[Michael@devserver ~]$ grep -rnw '/var/www/concrete5.7.5.9/concrete/blocks/html' -e "BlockController"
/var/www/concrete5.7.5.9/concrete/blocks/html/controller.php:5:use \Concrete\Core\Block\BlockController;
/var/www/concrete5.7.5.9/concrete/blocks/html/controller.php:7:class Controller extends BlockController
[Michael@devserver ~]$

I also tried escaping the backslash to no avail.

[Michael@devserver ~]$ grep -rnw '/var/www/concrete5.7.5.9/' -e "Concrete\\Core\\Block\\BlockController"
[Michael@devserver ~]$

Also tried single quotes to no avail.

[Michael@devserver ~]$ grep -rnw '/var/www/concrete5.7.5.9/' -e 'Concrete\Core\Block\BlockController'
[Michael@devserver ~]$

How to find all files containing specific text which includes a backslash on Linux?

Community
  • 1
  • 1
user1032531
  • 24,767
  • 68
  • 217
  • 387

3 Answers3

1

Use single quotes and escape backslash.

grep -rnw '/var/www/concrete5.7.5.9/' -e'Concrete\\Core\\Block\\BlockController'
  • Yea, that is what I found as well. What does the `-e` do? Thanks – user1032531 Oct 05 '16 at 16:10
  • -e takes a regexp pattern, backslash is escape character in regexp, so you have to put 2 backslashes in order to scape backslash, if you use double quotes you have to use 4 backslaches , because backslash is the scape character in bash too. – Kendru Estrada Oct 05 '16 at 16:23
0

Use single quotes.

grep -rnw '/var/www/concrete5.7.5.9/' -e 'Concrete\Core\Block\BlockController'
Smithson
  • 141
  • 4
  • Had already tried it, but no go. But then I went one step further and tried to use single quotes and escape the backslashes with another backslash! Maybe should update your answer? Thanks! – user1032531 Oct 05 '16 at 16:02
  • I thought about it, but was lazy to check. – Smithson Oct 05 '16 at 16:10
  • Turns out they are needed. What had stumped me was I had to both change from double to single quotes and escape the backslashes. I tried each, but not at the same time :( – user1032531 Oct 05 '16 at 16:11
0

Grep has an option -F to interpret the pattern literally and not as a regular expression. For example:

$ cat infile
use \Concrete\Core\Block\BlockController;
class Controller extends BlockController
$ grep 'Concrete\Core\Block\BlockController' infile     # No match!
$ grep -F 'Concrete\Core\Block\BlockController' infile  # Matches!
use \Concrete\Core\Block\BlockController;
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116