grep, maybe

mark prgrmr at gmail.com
Thu Oct 29 13:31:47 EDT 2009


On Thu, Oct 29, 2009 at 11:38 AM, Maurice <mauricep at cds-cumberland.org>wrote:

> Looking for some guidance;
>
> I have several files within several folders (5 files per folder, and
> thousands of folders) that I need to search a text file within each
> folder for a word match (like three_little_pigs.txt, and I need to find
> "moe", if he's listed) and then when a match is found I need to move
> (not copy) that entire folder (and it's 3~5 files contained within) to
> another location...
>
>

#!/bin/sh
cd [top level directory]
grep -l -r ["search string in double quotes"] > /tmp/file_names_found
2>/tmp/grep.errs
cat /tmp/file_names_found|xargs -i basename {} >/tmp/dir_names_to_move
dir_list=`sort /tmp/dir_names_to_move|unique`
for DIRECTORY in $("dir_list"); do
     mvdir $DIRECTORY [new location here]
     STATUS=$?
     if [ $STATUS -ne 0 ]; then
        echo "mvdir returned status $STATUS"
     end
done
exit

You'll have to come up with a way to create the value for "new location
here" that is unique so you don't overlay all the directories into the same
path and lose everything, so maybe test it with a copy first.  You'll also
want to check /tmp/grep.errs for any error messages.  I hope this helps.

mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20091029/2cbd5c8d/attachment.html 


More information about the gnhlug-discuss mailing list