Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep a list of specific files [closed]

Tags:

grep

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.

like image 326
Allan Bowe Avatar asked Sep 12 '12 13:09

Allan Bowe


People also ask

How do I grep specific files?

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.

Does grep search hidden files?

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.

How do I grep multiple 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.

How do I grep all files in a directory for a string?

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.


1 Answers

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.

like image 197
Erin Stanfill Avatar answered Sep 25 '22 14:09

Erin Stanfill