Directory structures looks like this:



Diff'd with



I get the correct diff between these files and save it in a file but applying it has proved to be non-trivial.

I've tried patch -d orig/ < patch.diff but the -d expects the files that are to be patched should live immediately in orig

Is there some way I can have patch recur on a directory and apply the patch to the specified files that exist in the patch?

  • 1
    Look into the -p flag to tell patch your directory structure is different
    – Jeff Schaller
    Apr 14 '16 at 23:07
  • @JeffSchaller I have two different directories that have two different levels.
    – cbrad
    Apr 14 '16 at 23:10
  • @cbrad You misunderstand what the -p flag does. Read man patch, specifically the -p option. Apr 14 '16 at 23:19

While the given pathnames do not look quite right (especially the new tree, which someone else has changed), you can keep in mind that the patch program can be told to ignore a given number of levels of directory from the output of diff using the -p option.

When applying a patch to files in a different directory than the patch-file shows, you would cd into the directory (to cancel one part of the mismatch) and use the -p option to adjust for the cd.

Here is a short script illustrating how I would solve this (using the dry-run option of GNU patch for a simple listing):

cd /tmp/foo
rm -f $OUT
diff -u orig/l1/l2/file2patch new/l1/l2/file2patch >>$OUT
diff -u orig/l1-2/file2patch new/l1-2/file2patch >>$OUT

diffstat -p1 $OUT
cd orig
patch -p1 --dry-run <$OUT


$ find . -type f


$ /tmp/xx
 l1-2/file2patch  |   19 +++++++++++++------
 l1/l2/file2patch |    5 ++---
 2 files changed, 15 insertions(+), 9 deletions(-)
patching file l1/l2/file2patch
patching file l1-2/file2patch

Further reading:

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.