#!/bin/ksh ############################################################# # # Looking for a file with a certain pattern? But you can't # remember where the file is, but know its somewhere in or # below the current directory. This is for you. # # You do not give filenames as the setup is to look at all # files in the cwd, or look in cwd and all subdirectories. # # If you want only to look at files in the current directory # and not any subdirectories DON'T use the R option. # DEFAULT is to do only files in the current directory. # # Should be able to use all grep capabilities. # # SYNTAX: # grepr [-myopt [d|v|R]] [arguments_for_grep] pattern # # DO **NOT** give a filename as the last/any argument # # The R tells it to go recursively, else it will # look only at files in the current directory. # # EXAMPLES: # grepr -myopt R -b pattern # grepr -myopt vR pattern # grepr -v pattern # grepr -vb pattern # # OTHER POSSIBILITIES: # EXAMPLES from the command line: # # ### Won't do binary and other data type files, just text. # alias grepr 'grep \!* `find . -type f -print`' # # ### To do binary and other data type files # ### but does have problems with control characters # ### Maybe pipe to 'tr -d '\xx''. (Limited help.) # alias egrepr 'egrep \!* `find . -type f -print`' # # LIMITATION: # Does not look at ALL non-text files (ONLY looks at # FrameMaker, PDF, Microsoft, and compiled C code data # formats (binary), graphics, and compressed files). # # Does not look at 'dot' files (.login for example) as # only an 'ls' is done. # # BUGS: # It does runs VERY slowly - intensive for the filesystem. # # Written by: Matt Baker # Created on: 17 Jun 93 # Last edited on: 10/1/2009 # Version 3.5 # # Tested on following: # CPU: Sun 3, Sparc, DECstation 5000, Alpha and many others # OS: SunOS 4.1.1, Ultrix 4.1, OSF 3.2, Solaris 2.X and many others # ############################################################# ################### # Check to see if at least one command line argument (pattern) ################### if [ $# -lt 1 ] then echo "Usage: $0 [-myopt [d|v|R]] {grep_options} pattern" echo "DO **NOT** give a filename as the last/any argument" exit 1 fi ################### # set recursion var and shift so its # not part of the options (resulting # in incorrect grep option) ################### RECURSION=0 DEBUG=0 VERBOSE=0 if [ "$1" = "-myopt" ] then shift ARGS="$1" shift if [[ -n $(echo $ARGS | grep R) ]] then RECURSION=1 fi if [[ -n $(echo $ARGS | grep v) ]] then VERBOSE=1 fi if [[ -n $(echo $ARGS | grep d) ]] then DEBUG=1 fi fi if (( DEBUG )) then set -x fi OPT="$*" ######################### # Declare function 'lookup' ######################### ################### # ORDER OF EVENTS # Get current pwd ls of files # IF: [directory] and R => call function and repeat (recursive) # ELSE IF: if [binary,data,framemaker,graphics] => strings | grep # ELSE IF: if [compress] => zcat | grep # ELSE: (plain file) grep ################### lookup () { if (( DEBUG )) then set -x fi # get rid of . and .. from list for FILE in $(ls -a | egrep -v '\.|\.\.') do if (( VERBOSE )) then echo "... looking at $FILE ..." fi ################ # for data/binary test ################ FILE_TYPE=$(file "$FILE" | awk -F: '{print $2}') ################ # if directory, recursion, if not set ################ if [[ -d $FILE && $RECURSION == 1 ]] then if (( VERBOSE )) then echo "... going into DIRECTORY $FILE ..." fi cd $FILE > /dev/null 2>&1 lookup cd .. ################ # FrameMaker, SunOS compiled code, graphics, OSF compiled code # added PDF, Microsoft ################ elif [[ -n $(echo $FILE_TYPE | egrep -e \ 'executable|Frame|Microsoft|PDF|rasterfile|data|demand' \ | grep -v script ) ]] then OUTPUT="$(strings "$FILE" | grep $OPT)" if [[ -n $OUTPUT ]] then echo "$(pwd)/$FILE:" echo "$OUTPUT" echo "" fi ################ # Z compressed ################ elif [ "$(echo $FILE_TYPE | grep -v gzip | awk '{print $2}')" = "compressed" ] then OUTPUT="$(zcat $FILE | grep $OPT)" if [[ -n $OUTPUT ]] then echo "$(pwd)/$FILE:" echo "$OUTPUT" echo "" fi ################ # gz compressed ################ elif [ "$(echo $FILE_TYPE | awk '{print $2}')" = "gzip" ] then OUTPUT="$(zcat $FILE | grep $OPT)" if [[ -n $OUTPUT ]] then echo "$(pwd)/$FILE:" echo "$OUTPUT" echo "" fi ################ # normal text file ################ else OUTPUT="$(grep $OPT $FILE)" #OUTPUT="$(grep $OPT $FILE)" if [[ -n $OUTPUT ]] then echo "$(pwd)/$FILE:" echo "$OUTPUT" echo "" fi fi done } if (( DEBUG )) then set -x fi ##################### # Call function ##################### lookup ##################### # Remove temp file ##################### if [ -s /tmp/$$ ] then rm /tmp/$$ fi