- [Instructor] Welcome to the next video of section three,
finding the difference between files, patching.
In the previous video, we've learned how
to create ISO files in hybrid ISO.
This video illustrates how to generate differences
between files highlighted with line numbers.
When multiple versions of a file are available,
it's very useful when we can find
the differences between files being highlighted
rather than comparing two files manually.
If the files are large, it is very difficult
and time-consuming to compare them by hand.
When working on large files by multiple developers,
when one of them has made changes,
and these changes need to be shown to the other,
sending the entire source code
to other developers manually is time-consuming.
Sending a difference file instead is very helpful,
as it consists of only lines which are changed,
or added, or removed, and line numbers are attached with it.
The difference file is called a patch file.
We can add the changes specified in the patch file
to the original source code by using the patch command.
We can also revert the changes by patching again.
Let's see how to do this.
The diff command utility is used
to generate difference files.
To generate difference information,
create the following files.
File one, file two.
Non-unified diff output without the minus-U flag
will be as follows.
The unified diff output will be as follows.
The minus-U option is used to produce a unified output.
Everyone prefers unified output,
as unified output is more readable
and because it's easy to interpret the difference
that is being made between two files.
In unified diff, the lines starting with plus
are the newly added lines,
and lines starting with minus are the removed lines.
A patch file can be generated by redirecting
the diff output to a file as follows.
Now, using the patch command, we can apply changes
to any of the two files.
When applied to Version1.txt, we get Version2.txt.
When applied to Version2.txt, we receive Version1.txt.
To apply the patch, use the following command.
We know have Version1.txt with the same content
as that of Version2.txt.
To revert the changes back, use the following command.
As shown, patching an already patched file
reverts back the changes.
To avoid prompting the user with yes/no,
we can use the minus-capital R option
along with the patch command.
Let's go through additional features available with diff.
Generating difference against directories.
The diff command can also act
recursively against directories.
It will generate a difference output for
all the descendant files in the directories.
Use the following command.
The interpretation of each of
the previous options is as follows.
Minus-capital N is for treating absent files as empty.
Minus-A is to consider all files as text files.
Minus-U is to produce unified output.
Minus-R is to recursively traverse
through the files in the directories.
Awesome, we've now successfully learned
how to find the difference between files and patching.
In the next video, we'll learn about
using head and tail for printing the last or first 10 lines.