I would typically NOT use Python for doing recursive grep operations, because it is going to be SUPER slow compared to using ripgrep in Linux, or findstr in Windows. I have various shell scripts that use ripgrep at their core, and it is blazing fast. The built-in findstr command in Windows is not bad either.
Nonetheless, for small tasks where monster shares are not being searched, a quick Python solution may come in handy in certain situations, and below is how it can be done. The goal here is to recursively search for a string of text and return each filepath that has “hits”.
from pathlib import Path
search_term = “password”
start_point = Path(“/tmp”)
for file in start_point.rglob(“*”):
if not file.is_file():
continue
file_content = file.read_bytes()
if search_term.encode() in file_content:
print(str(file))
Output from my Windows computer is included below. I only staged one test file there with the string password in it, but look how many files it found. I forgot that I had left so much in that folder.
\tmp\test1.txt
\tmp\tmp\c2-agent.c
\tmp\tmp\client.py
\tmp\tmp\server.py
\tmp\pyinstaller-develop\bootloader\Vagrantfile
\tmp\pyinstaller-develop\doc\bootloader-building.rst
\tmp\pyinstaller-develop\.github\ISSUE_TEMPLATE\antivirus.md
\tmp\pyinstaller-develop\tests\functional\test_django.py
\tmp\pyinstaller-develop\tests\functional\test_libraries.py
\tmp\pyinstaller-develop\tests\functional\data\django\db.sqlite3
\tmp\pyinstaller-develop\tests\functional\data\django\django_site\settings.py