Linux/patch

From Omnia
(Redirected from Patch)
Jump to navigation Jump to search

patch and diff

diff from another directory

no built in method, so have to get tricky

Files for test:

  • test.original
  • test.fixed
  • test # target file

Option #1 - move to correct path manually

cd ~/test ; cp test.fixed test
cd .. ; diff -u test/test.original test/test | tee test.patch
#  --- test/test.original  2026-04-21 09:20:42.864135487 -0700
#  +++ test/test   2026-04-21 09:20:56.014023558 -0700
# Then if could be used like:
cp test/test.original test/test
cat test.patch | patch -d . -p0
#  patching file test/test

Option #2 - use the -pX to drop path components

cd ~/test ; cp test.fixed test
diff -u ../test/test.original ../test/test | tee test.patch
#  --- ../test/test.original       2026-04-21 09:20:42.864135487 -0700
#  +++ ../test/test        2026-04-21 09:25:49.926535912 -0700
# Then if could be used like:
cp test.original test
cat test.patch | patch -d .. -p1  # p1 drops first path component
#  patching file test/test
# Alternative would be to edit the patch file and remove the "../" from the header

Option #3 - edit the header

cd ~/test ; cp test.fixed test
diff -u ~/test/test.original ~/test/test | tee test.patch
#  --- /home/USER/test/test.original    2026-04-21 09:20:42.864135487 -0700
#  +++ /home/USER/test/test     2026-04-21 09:31:31.177670259 -0700
# then edit the patch file and fix the header paths
#  --- test/test.original    2026-04-21 09:20:42.864135487 -0700
#  +++ test/test     2026-04-21 09:31:31.177670259 -0700
cp test.original test
cat test.patch | patch -d .. -p0
#  patching file test/test

see also

keywords