Suppose I have a collection of files like this:
echo -e "apple\n cherry" >> f1.txt;
echo -e "grape\n strawberry" >> f2.txt;
echo -e "orange\n cherry" >> f3.txt;
And I want to find each file that has the word “cherry” in it. I can do this:
ls *.txt | xargs cat | grep cherry
But this just returns the two lines that match from f1 and f3, without showing the “source” files, like so:
cherry
cherry
I’d like is to see the files where these matches came from—something like this:
f1.txt -> cherry
f3.txt -> cherry
Is there a way to do this simply from the command line?
1 Answer