I have a list of files generated using find that I want to feed (pipe) to cp. My problem is that the files have spaces and apostrophes in them, leading cp to repeatedly complain that it "cannot stat". Is there an elegant solution to this problem?

  • Check out xargs --null or -0 argument. – jasonwryan Sep 23 '12 at 8:25
  • I have but I don't see how it helps; I need to use printf with find. – Emre Sep 23 '12 at 8:29
  • 2
    @Emre: can you please post the full command you are trying to run? – Francesco Turco Sep 23 '12 at 9:20
  • 3
    Why do you need to use printf? Do you mean the printf command or the -printf option to find? Note that find ... -print0 is equivalent to find ... -printf '%p\0' – Keith Thompson Sep 23 '12 at 9:58
  • I was attempting to replace empty files that did not get copied properly by using find until I discovered rsync ... – Emre Sep 24 '12 at 5:27

Make sure you have -print0 in the find command.

find . -print0 | xargs -0 cmd ...
| improve this answer | |

Another option is to skip -print0 and the pipe, and instead use the -exec cp {} path/to/dest/ + or -exec cp {} path/to/dest \; commands to find. The first replaces {} with a whole bunch of found filenames at once; the second copies found files one-at-a-time. These commands are present on most find implementations.

| improve this answer | |

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.