Is it possible to apply patch to different file name but with the same structure. The case was, I have a patch of a file with specific name and then for some inevitable reason that file got changed and duplicated into multiple files but I have to apply the patch to all of that files, besides that I'm not allowed to rename that many files into the original name first.

Here's the example: (it's just an example, actual case a bit different)


  • original file main.cpp
  • copied and duplicated into main_cli.cpp, main_tmp.cpp, and a few main_*.cpp


  • original file main.cpp
  • got modified
  • create a patch (using diff -u)
  • get a patch file main.patch

So I have to apply main.patch from local-pc to all main_*.cpp files in server-pc.

Thank you.

1 Answer 1


You can specify that the patch is to be applied to a specific file:

patch -i main.patch main_cli.cpp

Then, it's simple enough to loop over all the files:

for f in main_*.cpp; do patch -i main.patch "$f"; done


$ diff -u bar foo > patch1
$ cp bar bar2
$ patch < patch1
patching file bar
$ patch -i patch1 bar2
patching file bar2
$ diff -s foo bar2
Files foo and bar2 are identical

Note how patch -i patch1 bar2 patched bar2 even though the patch file specified bar.

  • Work like charm, you really save my day, Thank you. I know this comment was worthless and meaningless but I had to say my gratitude :) Apr 7, 2018 at 5:01

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.