Cameleon
Cameleon

Reputation: 59

Execute loop on bash thru keywords

I have such script, it search my mail files and if keyword is found it move all files to other location.

How to make it work for multiple keywords?, for example i would have 11 KEY's and i would not want to copy and paste find command over and over.

DIRF='move/from'
DIRT='move/to'
KEY='discount'

find $DIRF -type f -exec grep -ilR "$KEY" {} \; | xargs -I % mv % $DIRT

Upvotes: 1

Views: 36

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 80921

Why are you using find here at all?

You are already telling grep to operate recursively (-R) so just point it at $DIRF and be done. -R is also pointless if you only ever give it files (from type -f).

Also grep takes a pattern that can do alternation. Just use that.

grep -RilE 'KEY1|KEY2|KEY3|Key4' "$DIRF"

Upvotes: 3

Luc M
Luc M

Reputation: 17314

for KEY in "discount"  "other_value" "other_value2"
do
    find $DIRF -type f -exec grep -ilR "$KEY " {} \; | xargs -I % mv % $DIRT
done

Upvotes: 1

Related Questions