Given this example folder structure:
/folder1/file1.txt /folder1/file2.djd /folder2/file3.txt /folder2/file2.fha
How do I do a recursive text search on all *.txt
files with grep
from "/"?
("grep -r <pattern> *.txt"
fails when run from "/", since there are no .txt
files in that folder.)
My version of GNU Grep has a switch for this:
Described as follows:
If you have a large number of files it would be useful to incorporate xargs into the command to avoid an 'Argument list too long' error.
you might be able to make use of your
zsh
'sEXTENDED_GLOB
option (docs)You may want to take a look at ack at http://betterthangrep.com, which has facilities for selecting files to search by filetype.
Mannis answer would fork a new grep-process for every textfile. If you have lots of textfiles there, you might consider grepping every file first and pick the .txt-files when thats done:
That's more disk-intensive, but might be faster anyway.
It's 2019 and there's no way I would still use grep for recursive text searching.
IMHO todays answers should include ripgrep: