Question

Here's an output of diff -u "temp temp/docs 1.txt" "temp temp/docs 2.txt":

--- temp temp/docs 1.txt    Mon Apr  7 16:15:08 2014
+++ temp temp/docs 2.txt    Mon Apr  7 16:18:45 2014
@@ -2,6 +2,6 @@
 22
 333
 4444
-555555
+55555
 666666
 7777777 

However, feeding this diff to patch -u fails with following message:

can't find file to patch at input line 3
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|--- temp temp/docs 1.txt       Mon Apr  7 16:15:08 2014
|+++ temp temp/docs 2.txt       Mon Apr  7 16:18:45 2014
--------------------------

Apparently, the spaces are the problem; is there a way to make patch to work on files with spaces in names?

Was it helpful?

Solution

No, GNU patch doesn't support this. Here's the official statement: http://www.gnu.org/software/diffutils/manual/html_node/Unusual-File-Names.html#Unusual%20File%20Names

OTHER TIPS

Gnu patch 2.6.1 (linux) seems to obey at least 1 space (not tried with more) if the filename is separated from the date with tab.

YYMV

I encountered the same problem when trying to establish conventions how to do manual version control with diff and patch.

I found out that GNU "diff" creates quoted path names in the patch headers if they contain spaces, while BusyBox "diff" doesn't.

Neither GNU nor BusyBox "patch" accepts quoted path names.

If the problem is just embedded spaces within filenames, it can therefore be avoided by using "busybox patch" rather than GNU "patch".

Another solution is to postprocess the output of GNU "diff" before feeding it into "patch":

sed 's,^\([-+]\{3\} \)"\([^"]*\)",\1\2,' $PATCHFILE | patch -p1

This works whether $PATCHFILE was created with GNU or busybox diff, but will only work with unified diff format.

Unfortunately, it turns out that leading or trailing spaces in filenames cannot be preserved with this method, as "patch" will skip them when parsing the path names from the patch instructions.

The approach will neither work if the filename starts with a literal double quote - but then, who uses such file names?

Most of the time, however, the above approach works just fine.

Finally a note of other approaches I have also tried but which did not work:

First I tried to replace the quotation of the whole path names by individually quoted path name components. This failed because "patch" does not use double quotes as meta-characters at all. It considers them to be normal literal characters.

Then I tried to replace all spaces by "\040" like CVS does - but "patch" does not seem to accept octal-escapes either, and this failed too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top