Grep specific file list

I need grep for a string, but only inside specific files in a directory, for example:

grep -rl mystring "file1.txt file2.txt file3.blah"

What is the correct syntax? I work in Linux OS.

+5
source share
2 answers

Drop the quotation marks around the file names so that they are treated as separate parameters. Also, I don’t think you need -r because you are just specifying files, not folders.

grep  mystring file1.txt file2.txt file3.blah

You can check http://ss64.com/bash/grep.html (or man grep) for other examples.

+10
source

You can simply provide a list without quotes, it will list all the results with the file in which they are located. eg:

grep init foo.py bar.py
foo.py:    def __init__(self, label, active, filter, filter_string):
foo.py:    def __init__(self):
bar.py:    def __init__(self, prefs, num_panes):
+1
source

All Articles