Need a little help, I need correct syntax for grep and copy

66 Views Asked by At

I currently have 3TB of data on a disk with small to medium files in hundreds of folders. I need to find certain text files witch contain certain words ( more than one word ). I've already tried grep-ping for them. This works as it prints the path to every file. But this is a long list and I'm now looking for a workable way to copy them to another folder.

any ideas ?

Is there some way to put -exec cp -rv /estinationfolder in the syntax and have it copy all results to the folder ?

1

There are 1 best solutions below

1
On

Yes , certainly there is a way.

You can pipe the grep output to copy command and provide required destination directory.

Here is a example,

find . -type f | xargs grep -l "textToSearch" | cpio -pV $destination_path

this script will copy files to destination path provided in destination_path variable

Best part with this is, it will copy the files while preserving the full path.