Grep All Files In A Directory
This question already has an answer here:
@Chris it's possible you don't have.scss files in current directory but somewhere deeper in subdirs so grep does not look in all the files you wanted. You should use -include option to tell grep to look recursively for files that matches specific patterns: grep -r x -include '.scss'. May 26, 2016 - You need the -d skip option added on. Grep is searching inside of files. You can search recursively, as you said, if you want to search files inside of a directory. By default, grep will read all files, and it detects the directories. Searching just within the parent directory would be `grep -d skip 'string'./. Grep -r 'word'. Grep -r 'string'. To ignore case distinctions: grep -ri 'word'. To display print only the filenames with GNU grep, enter: grep -r -l 'foo'. You can also specify directory name: grep -r -l 'foo' /path/to/dir/.c. Find command: Recursively Search All Files For A String.
- How to search files where two different words exist? 5 answers
I am trying to figure out the correct syntax to find two strings, the entire part of each string, anywhere (doesn't have to be near each other) in a file. So any file that has both foo
and say the number 321
, doesn't have to be alone and can be a substring should match. I've tried the following without much luck:
marked as duplicate by Stéphane Chazelas, vonbrand, rahmu, manatwork, n0peMar 16 '13 at 15:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
migrated from serverfault.comMar 16 '13 at 9:05
This question came from our site for system and network administrators.
5 Answers
Should be a little faster because the second grep
may operate on a list of files.
find
is more useful if you want to search recursive directories (in that case lose the -mindepth
and -maxdepth
options.
You can do this with a short script:
You can also do this on one line:
grep
returns 0 (true) if it found the string and the &&
separating the commands means that the second one will only run if the first one was true. The -q
option makes sure that grep
does not output anything.
The echo will only run if both strings were found in the same file.
I thought of a different way to do it. This way will probably be more efficient if the files in question are larger than your installed RAM as it only has to grep
through each file once.
and the one-line version:
Strange. For me both variants work (grep (GNU grep) 2.13):
Edit 1 - show files with both matches only
The for file in *
answer works but can become a performance nightmare (for big amounts of files): at least two processes per file. This is faster (in the GNU world):
string1 should be the one which results in fewer matches.
Hauke LagingGrep In All Subdirectories
Hauke LagingBasically, to find all files including a particular string in a directory, you can use:
-l
: to make this scanning will stop on the first match-i
: to ignore case distinctions in both the pattern and the inputfiles-r
: search all files under directory, recursively
To search for two patterns, try this:
Grep All Files In Directory With Extension
quantaquantaShould be
Use -e for multiple patterns
EDIT
In case you need both to match:
If the order does not matter: