开发者

How to send cp a list of filenames (with spaces)

Egrep is generating for me a list of files based on their contents, but I'm doing something wrong trying to pass that list to cp as arguments. (My shell is bash). I thought I'd escape the spaces in the filenames and convert the newlines to spaces, but cp appears to ignore the escaped spaces in the piped input.

Example filename: 2011-05-15\ 14.43.41.txt

$ cp `egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' '` ~/crashreportstemp/

cp: cannot stat `2011-05-15': No such file or directory

When I execute the contents of the backticks, I'm getting output that I could paste right into cp.

I've tried using xargs too:

$ egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' ' | xargs cp ~/crashreportstemp

But that causes cp to treat the last passed file name as the final cp argumen开发者_如何学Got, ignoring my explicit argument to cp:

cp: target `2011-05-30 16.23.30.txt' is not a directory

I'm obviously overlooking the right way to do this, please help!

Thanks - Jason


Try something like this:

egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' ' && echo ~/crashreportstemp | xargs cp

In your version, ~/crashreportstemp is getting passed as the first argument to cp, not the last.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜