Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...