Apply multiple .patch files

Assuming you're using bash/sh/zsh etc...

cd /path/to/source
for i in /path/to/patches/*.patch; do patch -p1 < $i; done

Accepted answer did not work for me, it seems to assume patch can take multiple patch files on one command line. My solution:

find /tmp/patches -type f -name '*.patch' -print0 | sort -z | xargs -t -0 -n 1 patch -p0 -i

Find: Finds patch files

  • /tmp/patches: The directory to search for patch files in
  • -type f: only files
  • -name '*.patch': files which end in .patch
  • -print0: output results to stdout as a list of null-terminated strings

Sort: Sorts patch files so order remains (e.g. 001 comes before 002)

  • -z: input is null terminated (since we used -print0)

xargs: Call patch using stdin as arguments

  • -t: Print command before running it, can remove it for less verbosity
  • -0: stdin is a list that is null terminated
  • -n 1: Call patch again for every 1 item in the list (e.g. call patch N times, instead of building a list and calling it once)
  • -i: argument to patch to tell it the argument will be the patchfile

If cat works, why not use it?

To use find and xargs:

find dirname -name namespec -print0 | xargs -0 patch patchargs

Example:

find src/networking -type f -name 'network*.patch' -print0 | xargs -0 patch -p2