0

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 argument, 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

rockriver
  • 51
  • 1
  • 3

1 Answers1

0

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Thanks! Good point. It's almost working, but cp seems unable to discern that the last argument is the destination. "cp: missing destination file operand after `~/crashreportstemp'" If I copy the output of the command before the xargs pipe and paste it in front of the cp command, it works OK. Maybe I need to use input redirection? – rockriver May 31 '11 at 18:26