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...
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/