Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Patching files in unix using diff and patch commands
1. Patching files in Unix
with diff and patch commands
Alberto Bustamante Reyes
http://alb3rtobr.github.io
2. • Compares two files and gives the necessary steps to
be applied to the first one to become the same as the
second one.
diff command
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
a.txt b.txt
3. diff example
$-> diff a.txt c.txt
1,2c1,2
< I need to buy:
< Apples
---
> You need to buy:
> Red Apples
4d3
< Bread
5a5
> Cookies
1
2
3
4
5
6
1
2
3
4
5
6
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
4. diff example
$-> diff a.txt c.txt
1,2c1,2
< I need to buy:
< Apples
---
> You need to buy:
> Red Apples
4d3
< Bread
5a5
> Cookies
1
2
3
4
5
6
1
2
3
4
5
6
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
List of differences
5. diff example
$-> diff a.txt c.txt
1,2c1,2
< I need to buy:
< Apples
---
> You need to buy:
> Red Apples
4d3
< Bread
5a5
> Cookies
1
2
3
4
5
6
1
2
3
4
5
6
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
Change line 1 to 2 of a.txt by
line 1 to 2 of b.txt
6. diff example
$-> diff a.txt c.txt
1,2c1,2
< I need to buy:
< Apples
---
> You need to buy:
> Red Apples
4d3
< Bread
5a5
> Cookies
1
2
3
4
5
6
1
2
3
4
5
6
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
Delete from line 4 to 3 (not
included)
7. diff example
$-> diff a.txt c.txt
1,2c1,2
< I need to buy:
< Apples
---
> You need to buy:
> Red Apples
4d3
< Bread
5a5
> Cookies
1
2
3
4
5
6
1
2
3
4
5
6
I need to buy:
Apples
Cheese
Bread
Butter
Oranges
You need to buy:
Red Apples
Cheese
Butter
Cookies
Oranges
Add line 5
8. diff example (context)
$ -> diff -c a.txt b.txt
*** a.txt Thu Oct 23 12:39:32 2014
--- b.txt Thu Oct 23 12:39:47 2014
***************
*** 1,6 ****
! I need to buy:
! Apples
Cheese
- Bread
Butter
Oranges
--- 1,6 ----
! You need to buy:
! Red Apples
Cheese
Butter
+ Cookies
Oranges
9. diff example (context)
$ -> diff -c a.txt b.txt
*** a.txt Thu Oct 23 12:39:32 2014
--- b.txt Thu Oct 23 12:39:47 2014
***************
*** 1,6 ****
! I need to buy:
! Apples
Cheese
- Bread
Butter
Oranges
--- 1,6 ----
! You need to buy:
! Red Apples
Cheese
Butter
+ Cookies
Oranges
List of differences in the context
of both files content
10. diff example (context)
$ -> diff -c a.txt b.txt
*** a.txt Thu Oct 23 12:39:32 2014
--- b.txt Thu Oct 23 12:39:47 2014
***************
*** 1,6 ****
! I need to buy:
! Apples
Cheese
- Bread
Butter
Oranges
--- 1,6 ----
! You need to buy:
! Red Apples
Cheese
Butter
+ Cookies
Oranges
Change
Change
11. diff example (context)
$ -> diff -c a.txt b.txt
*** a.txt Thu Oct 23 12:39:32 2014
--- b.txt Thu Oct 23 12:39:47 2014
***************
*** 1,6 ****
! I need to buy:
! Apples
Cheese
- Bread
Butter
Oranges
--- 1,6 ----
! You need to buy:
! Red Apples
Cheese
Butter
+ Cookies
Oranges
Delete
12. diff example (context)
$ -> diff -c a.txt b.txt
*** a.txt Thu Oct 23 12:39:32 2014
--- b.txt Thu Oct 23 12:39:47 2014
***************
*** 1,6 ****
! I need to buy:
! Apples
Cheese
- Bread
Butter
Oranges
--- 1,6 ----
! You need to buy:
! Red Apples
Cheese
Butter
+ Cookies
Oranges
Add
13. diff example (unified)
$ -> diff -u a.txt b.txt
--- a.txt 2014-10-23 15:54:50.588923000 +0200
+++ b.txt 2014-10-23 15:54:59.549414000 +0200
@@ -1,6 +1,6 @@
-I need to buy:
-Apples
+You need to buy:
+Red Apples
Cheese
-Bread
Butter
+Cookies
Oranges
14. diff example (unified)
$ -> diff -u a.txt b.txt
--- a.txt 2014-10-23 15:54:50.588923000 +0200
+++ b.txt 2014-10-23 15:54:59.549414000 +0200
@@ -1,6 +1,6 @@
-I need to buy:
-Apples
+You need to buy:
+Red Apples
Cheese
-Bread
Butter
+Cookies
Oranges
List of differences in a unified
way
15. • Uses the diff command output
• Apply a patch
• Revert a patch
• Very common error message:
Patch command
$ -> patch < a-to-b.patch
can't find file to patch at input line 1
Perhaps you used the wrong -p or --strip option?
File to patch:
$ -> diff -u a.txt b.txt > a-to-b.patch
$ -> patch < a-to-b.patch
patching file a.txt
$ -> patch –R < a-to-b.patch
16. • Common problem:
▫ User A creates a patch for /myapp/example/files/a.txt
▫ The header of the patch will contain the path of the file:
$ -> cat changes-in-a.patch
--- /myapp/example/files/a.txt.backup 2014-10-23 15:54:50.588923000 +0200
+++ /myapp/example/files/a.txt 2014-10-23 15:54:59.549414000 +0200
▫ User A sends the patch to User B.
▫ User B file is stored in a different path, i.e, /myapp/files/a.txt
▫ User B gets an error because patch command will try to apply
the changes in the path stated in the patch file header.
Strip (-p) option
17. › Indicates how many slashes are strip from the file names
found in the patch
› If a patch was generated for /myapp/example/files/a.txt the
patch command will try to apply it to the same path.
› Using strip option:
– p0 -> /myapp/example/files/a.txt
– p1 -> myapp/example/files/a.txt
– p2 -> example/files/a.txt
Strip (-p) option
$ -> patch –p<number> < file.patch