Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

grep - Linux command line: Find all files with a certain extension in a directory tree containing specific text

I would like to recursively traverse a directory tree and extract all files which contain a certain text in a remote Linux machine. I found a helpful command in this website:

grep -iRl "your-text-to-find" ./

Now however, I would like to modify this slightly by searching only in python (.py) files. I tried the following but it doesn't seem to work:

grep -iRl "your-text-to-find" ./*.py

How can I modify the command such that it recursively finds all .py files containing "your-text-to-find?

question from:https://stackoverflow.com/questions/66063925/linux-command-line-find-all-files-with-a-certain-extension-in-a-directory-tree

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After looking through a few extra posts including this one, I found a command which works.

I am not exactly sure what xargs does but the post I mentioned explains a bit more:

 find ./ -name *.py | xargs grep "text-to-find"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...