The following simple shell script, lookfor, uses find to look for all files in the specified directory hierarchy that have been modified within a certain time, and it passes the resulting names to grep to scan for a particular pattern. For example, the command:
% lookfor /work -7 tamale enchilada
would search through the entire /work filesystem and print the names of all files modified within the past week that contain the words "tamale" or "enchilada." (For example, if this article is stored in /work, lookfor should find it.)
The arguments to the script are the pathname of a directory hierarchy to
search in ($1
), a time ($2
), and one or more text patterns (the other
arguments). This simple but slow version will search for an (almost) unlimited
number of words:
#!/bin/sh temp=/tmp/lookfor$$ trap 'rm -f $temp; exit' 0 1 2 15 find $1 -mtime $2 -print > $temp shift; shift for word do grep -i "$word" `cat $temp` /dev/null done
That version runs grep once to search for each word. The -i option makes the search find either upper- or lowercase letters. Using /dev/null makes sure that grep will print the filename. Watch out, though: the list of filenames may get too long.
The next version is more limited but faster. It builds a
regular expression for egrep that finds all the words in one pass through the files. If
you use too many words, egrep will say
Regular
expression
too
long
. Also, your egrep may not have a -i
option; you can just omit it. This version also uses xargs; though xargs has its
problems.
#!/bin/sh where="$1" when="$2" shift; shift # Build egrep expression like (word1|word2|...) in $expr for word do case "$expr" in "") expr="($word" ;; *) expr="$expr|$word" ;; esac done expr="$expr)" find $where -mtime $when -print | xargs egrep -i "$expr" /dev/null
—JP and TOR