简体   繁体   中英

How to know which file holds grep result?

There is a directory which contains 100 text files. I used grep to search a given text in the directory as follow:

cat *.txt | grep Ya_Mahdi

and grep shows Ya_Mahdi .

I need to know which file holds the text. Is it possible?

Just get rid of cat and provide the list of files to grep :

grep Ya_Mahdi *.txt

While this would generally work, depending on the number of .txt files in that folder, the argument list for grep might get too large.

You can use find for a bullet proof solution:

find --maxdepth 1 -name '*.txt' -exec grep -H Ya_Mahdi {} +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM