Reputation: 61
I have thousands of folders filled with old junk that I need to make sense of. Namely I need to find all files containing specific text in files using mask. But if I do it using grep -R "mask" it churns all that junk and machine is stuck as a result. I want to pipe find and cat results to grep but can't find right way.
a=$(find ./ -name "*.txt")
for i in $a; do
cat $i | grep "string" >> result.txt
done
Something like that, but it doesn't seems to work. Where am I stupid?
Upvotes: 1
Views: 3608
Reputation: 72366
You don't need cat
.
grep "string" $i
is faster.
You don't need find
either.
The command you need is:
grep -r -l "string" --include="*.txt" .
It runs faster than any combination of find
and grep
because it runs only one process.
The arguments:
-r
tells grep
to process the input directory (.
) recursively.-l
tells it to suppress its normal output and echo only the name of the matching files. It runs faster because it stops on the first match in each file."string"
is the text you are searching.--include="*.txt"
limits the search to the files whose name match the pattern. The quotes around *.txt"
are needed; without them the shell expands *.txt
to the list of matching files in the current directory..
is the directory where to search for files; the current directory in this example.Upvotes: 3