Login to Members Area

Choose the service you want to log in
24x7x365 Presence
Live Chat

Hosting Blog


How to use diff and patch

We'll learn the three things below, as follows:-

1) Differentiating between two files and redirecting the differences into a new file.
2) Creating a patch file with the diff command utility.
3) Updating the file with patch.

diff => diff is a Unix utility showing the difference between two files which are almost similiar by virtue of their contents.

patch => patch is a Unix utility which is used for applying the differences between two files to make them similiar or the same by virtue of their contents.

1) With the diff command utiliy we can show the difference between two files on the screen or can also redirect them into a file which will create a patch for making the target file the same as the source file.

i) To get the output on the screen between two files, use the command:-

diff abc_file.txt xyz_file.txt

ii) To redirect the difference between two files into one file, use the command below:-

diff abc_file.txt xyz_file.txt  > diff_file.txt

2) Now, creating a patch file with the diff command utility is as below:-

diff -uN orig_file new_file > patch.orig_file

3) Applying the file with patch

patch -u new_file patch.orig_file

and then when you check new_file, it'll have all the changes done and it'll have the same contents as orig_file.txt.

Using and applying a patch is the quickest way to do many changes with just one command, when you need to change multiple files for the same changes. That's all.

Sachin Monday 10 February 2014 - 12:24 am | | Default

No comments

(optional field)
(optional field)

Comment moderation is enabled on this site. This means that your comment will not be visible until it has been approved by an editor.

Remember personal info?
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.