I need to grep for a string but only within certain files within a directory- eg:
grep -rl mystring "file1.txt file2.txt file3.blah"
what is the correct syntax? I am working in a Linux OS.
The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.
The grep command is a useful Linux command for performing file content search. It also enables us to recursively search through a specific directory, to find all files matching a given pattern. The “.” matches the current path, which includes both hidden files and non-hidden files.
To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.
To grep All Files in a Directory Recursively, we need to use -R option. When -R options is used, The Linux grep command will search given string in the specified directory and subdirectories inside that directory. If no folder name is given, grep command will search the string inside the current working directory.
Drop the quotes around the file names so that they are treated as separate parameters. Also, I don't think you need the -r since you are just specifying files and not folders.
grep mystring file1.txt file2.txt file3.blah
You might want to check out http://ss64.com/bash/grep.html (or man grep) for other examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With