Created
July 5, 2017 21:09
-
-
Save jack980517/1193d496526013f331a43ed13877b901 to your computer and use it in GitHub Desktop.
Search for a specific string in all files, not just text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os,sys | |
def usage(): | |
print 'Usage: whatcontains [-case] [-hex] stringToSearchFor [folderToSearchIn]' | |
exit() | |
if not 1<len(sys.argv)<6: usage() | |
case=False | |
hex=False | |
if '-case' in sys.argv: | |
case=True | |
sys.argv.remove('-case') | |
if '-hex' in sys.argv: | |
hex=True | |
sys.argv.remove('-hex') | |
if hex: stringToSearchFor=sys.argv[1].decode('hex') | |
else: stringToSearchFor=sys.argv[1] | |
if len(sys.argv)==2: folderToSearchIn='.' | |
else: folderToSearchIn=sys.argv[2] | |
for root, dirnames, filenames in os.walk(folderToSearchIn): | |
for i in filenames: | |
a=open(root+'/'+i,'rb').read() | |
condition=stringToSearchFor in a | |
if case: condition=condition or stringToSearchFor.upper() in a or stringToSearchFor.lower() in a | |
if condition: print root+os.path.sep+i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment