shell script to search and copy files

Hello

Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario:
I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new directory.

find . | xargs grep "TEST"
I use this file command in order to find the files that i need that have the word "test" inside. I need to run this from root in order to find all the files containing this word and then something that automaticaly copies all the files on a new directory.

Any idea?

Best Regards

Try this:

find . -print0 |xargs -0 grep -l 'TEXT' | xargs -I '{}' cp {} /dest/dir

Hi Franklin52. Could you explain this part

xargs -0 grep -l 'TEXT'

I don't understand what exactly is doing -0 and I can't find it with man

It's an option of the GNU version, the lines are terminated by a null character instead of a space.
If you have another version it can be ommited but it works only with file names without spaces:

find . -type f|xargs grep -l 'TEXT' | xargs -I '{}' cp {} /dest/dir

Ok thanks Franklin52 :slight_smile: