grep, maybe

Ken D'Ambrosio ken at jots.org
Fri Oct 30 08:15:20 EDT 2009


After a night's sleep, I realized I might even be able to make Ben happy:

#!/bin/sh
cd /path/to/toplevel/dir
find -type d | while read i
do
  grep moe "$i/*" > /dev/null && echo "$i"
done | while read d
do
  mv "$d" /path/to/destination || echo "mv for $d didn't work: $?"
done

-Ken

P.S.  It deals gracefully with spaces (I checked -- hadn't been sure about
how the second "read" would work).
P.P.S.  It does appear that find is terminated for the second loop.  (Or
so said the "ps" I threw into it to check.)
P.^3S.  For millions of hits, your memory might start to bog.  In which
case, if you expect that, it's definitely time to go with the temporary
file thing.

On Thu, October 29, 2009 1:31 pm, mark wrote:
> 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
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is believed to be clean.
>
> _______________________________________________
> gnhlug-discuss mailing list gnhlug-discuss at mail.gnhlug.org
> http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/
>
>




-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the gnhlug-discuss mailing list