How to use grep -e to match partial words?

Viewed 18489

If you use grep like

grep -rnw '/your/path/to/search/' -e 'n getFoo'

grep wouldn't find a file that contains "function getFoo()".

If you only search for 'getFoo' or else 'function getFoo', grep will find the file that contains the function.

So what's the best way to find a file containing part(s) of a string?

Thanks in advance!

2 Answers

You should remove the -w option which tells grep to only match whole words.

grep -rn '/your/path/to/search/' -e 'n getFoo'

will also search in between word boundaries.

The -w flag causes whole word matches so it clearly what you don't need.

So what's the best way to find a file containing part(s) of a string?

shopt -s globstar
grep -li 'string to search for' /path/to/search/**
shopt -u globstar
Related