#!/usr/bin/python ############################################################################################### # This script was written for www.philforhumanity.com. # If you have any questions, issues, or enhancement requests, please contact us via the website. # Use this script at your own risk. ############################################################################################### # System Libraries import argparse from subprocess import Popen, PIPE # System commands that save output to variable # Parse arugments parser = argparse.ArgumentParser() parser.add_argument("word", help="The word or pattern to grep for.") parser.add_argument("-i", help="Turn off case sensitivity.", action="store_true") args = parser.parse_args() word = args.word if args.i: case = "-i" else: case = "" # Variables command = 'find . -path ./.git -prune -o -type f -name "*" -print' # Ignore .git directories with "-path ./.git -prune -o" # Only get files with "-type f" delimiter = "======================================================================" filecount = 0 linecount = 0 # Find all files p = Popen(command , shell=True, stdout=PIPE, stderr=PIPE) #print "Return code: ", p.returncode out, err = p.communicate() #print out.rstrip() #print err.rstrip() files = out.rstrip() # For each file, grep for filepath in files.split('\n'): # Split by EOLN #print "A:" + filepath # Skip files that end with these extensions if filepath.endswith(("~",".bak",".zip",".gz",".msi",".doc",".ppt",".xls",".pack",".png",".jpg",".gif")): continue #print "B:" + filepath # Run "file" on the filepath and look for "text" command = 'file ' + filepath p = Popen(command , shell=True, stdout=PIPE, stderr=PIPE) out, err = p.communicate() if "text" not in out.rstrip(): continue #print "C:" + out.rstrip() # Grep command = 'grep -n ' + case + ' "' + word + '" "' + filepath + '"' #print "command: " + command p = Popen(command , shell=True, stdout=PIPE, stderr=PIPE) out, err = p.communicate() grepoutput = out.rstrip() if grepoutput: print delimiter print "File: " + filepath print grepoutput filecount = filecount + 1 linecount = linecount + grepoutput.count("\n")+1 # Print total results if (filecount > 0): print delimiter print ' Number of files =', filecount, '\n Number of lines =', linecount # Exit exit ()