user2856064
user2856064

Reputation: 553

How to grep specific *name* in subfolders in Linux?

To grep some string in all subfolders I can use:

grep -rl "test" /path

I would like to grep some specific string assuming that the first part and the second part of the string could be different (has got different begin and end) and is case insensitive. I tried to do something like this, but it doesn't work:

grep -rli "*test*" /path

Could you please tell me how to do this?

To be clear I would like to find all files (grep) including subfolders, where there is specific string, like e.g.:

"*test*"

Upvotes: 2

Views: 1464

Answers (3)

Alex Gidan
Alex Gidan

Reputation: 2679

You should try grep accepting perl regex:

grep -irlP ".*test.*" /path

The .* means matching (zero or more times) any characters except newline. More about meta-characters here

You can use almost any regex, so it's a really flexible solution to match complex patterns. For instance, if you searched for entire words only:

grep -rlP "\b(test)\b" /path

Of course there are limitations. For instance, multiline search with this method can be difficult.

Upvotes: 1

Deepak
Deepak

Reputation: 575

grep -ilr "string" path/to/subdir

Will search sub-strings as well. grep searches pattern - you don't need those asterisks (or dot asterisks as in my pre-edit answer).

If you want to search the exact word and not its presence in sub-strings, use -w option.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

You can try like this using the "--include" option:

grep -rli --include="test*" /path

From the man page of grep

--include=GLOB

Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).

Upvotes: 0

Related Questions