Linux “grep and copy”

|
| By Webner

Suppose I have a folder hierarchy as below:

f1 > f2

f2 > f3

f2 > f4

f4 > f5

I want to find all files in f1 or any of its descendant folder that contain text “I am a match” and copy them into f1’s parent folder (whatever that is).

To accomplish above, run any of following commands from within f1 folder:

i) cp `grep -rl “i am a match” *` ..

ii) grep -rl “i am a match” * | xargs -i cp {} ..

Leave a Reply

Your email address will not be published. Required fields are marked *