Linux/diff: Difference between revisions

From Omnia
Jump to navigation Jump to search
No edit summary
 
Line 10: Line 10:
== patch and diff ==
== patch and diff ==


=== diff from another directory ===
See [[Linux/patch]]
 
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

Latest revision as of 16:33, 21 April 2026

Color Diff

Color Diff with Less

diff --color=always | less -r
git diff --color=always | less -r

ref: https://unix.stackexchange.com/questions/19317/can-less-retain-colored-output

patch and diff

See Linux/patch