Linux “grep and copy”

Author - 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 {} ..

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.