Searching words with ‘grep’ in multiple files
Linux / Command line
One common issue while debugging, refactoring or just programming is when you are searching a word or sentence in a huge number of files and folders.
Several algorithms could be implemented but they always will reach a slow or quick reading of files. One by one until they found a match.
Fortunately GNU provide a powerful tool called ‘grep’. Basically filters the file lines searching a specific word. It uses an algorithm optimized to read files, some say that the real secret it is not to read at all.
This example will show you the matches in the file <filename>.
$ grep "foo" <filename>
Now we go a step ahead by adding some parameters to the ‘grep’ command in order to search in all the files and folders in our location.
$ grep -nHr
Finally this example will show you a list of lines with the file name followed by a number of line and the corresponding line with the match.
$ grep -nHr "frequency"
test/mpeg-freq-test.c:49: struct v4l2_frequency vf;
test/mpeg-freq-test.c:55: vf.frequency = f[cnt % 2] * 16;
test/mpeg-freq-test.c:59: perror("could not set frequency");
doc/README.radio:26: -f Tune to a specific frequency