Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Created August 14, 2017 22:51
Show Gist options
  • Save trafficinc/fa8a130cc9f8204e88710fad9ed836a9 to your computer and use it in GitHub Desktop.
Save trafficinc/fa8a130cc9f8204e88710fad9ed836a9 to your computer and use it in GitHub Desktop.
Python script to find any text in Files - Recursive
#!/usr/bin/python3
import os,sys
keyWord = sys.argv[1];
rootdir=('/var/www/mysite/public')
found = []
for folder, dirs, files in os.walk(rootdir):
for file in files:
#if file.endswith('.php'):
try:
fullpath = os.path.join(folder, file)
with open(fullpath, 'r') as f:
for line in f:
if keyWord in line:
print(fullpath)
found.extend('1')
break
except IOError:
pass
if (len(found) > 0):
print(len(found), " file(s)")
else:
print("There are no matches")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment