I've looked everywhere for an answer and nothing seems to be working out. In Git Bash, I need to figure out how to take files found with grep and copy them elsewhere. The instructions were to use the find, grep and exec commands to do so. I've found the files I need to copy using the grep command (i.e. grep -rli [search terms] *), so how do I put that in a find exec command to copy?


You could use :

find . -name "*.exe" -exec cp {} ~/Documents \;

Not sure why you need to use exec, but otherwise you could use for example:

find . -name "*.exe" | grep something | xargs cp -t ~/Documents
find . -name "*.exe" | grep something | xargs -I {} cp {} ~/Documents # more intuitive with placeholders
| improve this answer | |
  • Thank you, I corrected with ".*exe" – PJ127 Nov 20 '19 at 14:01
  • 1
    That's not what the OP needs. The OP is looking for search terms inside the file, not in the file name (grep -rli [search terms]). (Also, strictly speaking, piping find output to xargs should not be done without find … -print0 and xargs -0, otherwise this will break if file names contain newlines.) – slhck Nov 20 '19 at 14:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.