how to use cp find and grep together to copy a list of files using find with grep
I am getting a list of files using the following command:
find . type -f | xargs grep -l 19201020320 now I want to modify the above command so that the listing produced by the above command can be copied to a folder (say /tmp). Tried the following but did not work! find . type -f | xargs grep -l 19201020320 | xargs cp /tmp Got the error message: cp: copying multiple files, but last argument `./XYZ.20130701.060000' is not a directory Try `cp --help' for more information. |
Code:
find . -type f -name "*19201020320*" -exec cp '{}' /tmp \; |
My criteria is searching in files for a given string .. in this case "19201020320". Shouldn't -name in find search for files having file names "*19201020320*"? :)
|
Try this:
Code:
#!/bin/bash |
Try following anf see if it works
Code:
grep -l 19201020320 ./* | xargs -I {} cp {} ./tmp/ |
Quote:
Code:
find . type -f | xargs grep -l 19201020320 | xargs cp -t /tmp |
I am unsure as why find command is being used - Is using grep directly inefficient ?
|
Yeap. That's what I was looking for :)
|
I am a newbie trying to feel around linux .. its more of an issue of getting through things rather than performance :P
|
Quote:
Always remember to quote your variables too, for similar reasons. Code:
#!/bin/bash Of course we can also use xargs instead of the loop, since it can handle null separated input too. Code:
find . -type f -exec grep -lZ '19201020320' '{}' + | xargs -0 cp -t /tmp Code:
grep -lZR '19201020320' . | xargs -0 cp -t /tmp Finally, notice how I used the -t option in cp, to invert the order of the arguments and make the command simpler. I believe this is only available in gnu cp, however. PS: Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks. |
@David the H.
Thanks for showing my mistakes. your suggestions were truly insightful. I really appreciate that. :hattip: |
All times are GMT -5. The time now is 10:26 PM. |