General
cancel
Showing results for 
Search instead for 
Did you mean: 

Searching for new files with a specific string

SOLVED
Go to solution
Kelvin Arcelay
Occasional Visitor

Searching for new files with a specific string

Hello,

I am need to search a directory tree for all "new" files containing a specific string. For example, search all files in /usr/var created after 01/01/09 and have the "test" word.

I've been tinkering with find and grep but not gotten too far.

Thanks,
3 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Searching for new files with a specific string

Hi:

For your 'find' use the '-newer' option to find only files newer than 01/01/2009. Use the 'file' command to confine your queries only to "text" (not binary) files.

# cat .findit
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
touch -amt 010100002009 /tmp/myref
find ${DIR} -xdev -type f -newer /tmp/myref | while read FILE
do
[ $(file ${FILE} | grep -c ascii) -eq 0 ] && continue
grep "${PAT}" ${FILE} /dev/null
done
exit 0

...run as .findit /path_to_search pattern_to_find

Using '-type f' with find limits that which is returned to *files* as opposed to *directories*.

The script skips *binary* files and examines files of type *text* for the pattern_to_find.

Regards!

...JRF...
Kelvin Arcelay
Occasional Visitor

Re: Searching for new files with a specific string

JRF,

Thank you...so much.

KJA
Kelvin Arcelay
Occasional Visitor

Re: Searching for new files with a specific string

The example provided by JRF did the job.