SlideShare a Scribd company logo
1 of 18
Patching files in Unix
with diff and patch commands
Alberto Bustamante Reyes
http://alb3rtobr.github.io
• 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
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
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
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
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)
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
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
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
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
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
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
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
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
• 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
• 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
› 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
Thanks for watching!

More Related Content

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
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