copying files resulting from using grep into a new folder

I have a question concerning the concatenation of two processes in the terminal. I have a folder with over 50.000 files. I used grep to look for those files containing a specific term:

grep -inrl "Bioethik_Debatte" /var/cqp/upload/heideko/import_files

which gives me a huge list like this:

/var/cqp/upload/heideko/import_files/26629.vrt
/var/cqp/upload/heideko/import_files/32862.vrt

I need to copy the resulting files into a new folder. I was thinking something like:

grep -inrl "Bioethik_Debatte" /var/cqp/upload/heideko/import_files | cp * bio_files/

My try might not make any sense. I'm just starting with the terminal. I just want to copy the files resulting from my grep search into a new folder called bio_files. I realised that what I get from grep is just the names of the files. But I want to use those names as input for the cp command. Any help is highly appreciated.

1 Answer

Use xargs and the -t option of cp:

grep -inrl "Bioethik_Debatte" /var/cqp/upload/heideko/import_files | xargs cp -t bio_files/

If your files might have spaces in their names, then make everything null-delimited:

grep -inrlZ "Bioethik_Debatte" /var/cqp/upload/heideko/import_files | xargs -0 cp -t bio_files/
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like