The diff and patch Commands


The diff and patch Commands

The diff and patch commands are a pair so we will cover them here together. Because of how they work, we will cover diff first.

When using diff, you will take two files, the original template file and an updated file that produces an output file. This file will contain enough information within it to be able to reconstruct the updated file when only given the patch file and template file. This can be handy if you want to only send out updates to, for example, a book, as you could send the edits instead of sending the entire file and replacing it. For smaller files this may not be a big deal, but for larger files that are of substantial size, and may have multiple people working on them, using diff may be very helpful to you.

To send out only the new material in a file, use the diff command with the following syntax: diff <template file> <updated file> > <patchfile>. The patchfile can be any filename you specify.

In this example, you can see how to create a patchfile.

 >diff template.txt update.txt > patch.txt 

The difference between the template file and the update file will be the patchfile. Try this command to see if it works for you in large file environments. If you can master this technique, it can save time creating, editing, and transferring files that you are collaborating on with others.

The diff command is also useful when you just want to know if two copies of the same file are identical or not. If you find yourself with multiple copies of similar files, diff is a fast and easy way to find out if they are identical. If the files are not identical, you will be able to see what changes have been made between them. No output produced by diff means that there are no differences between the files.

The patch command is also useful. It can be thought of as phase two of using the diff command. Now that you have your template, update, and new patchfile, you can use the patch with the file you want to fix. This can be seen as

 >patch template.txt patch.txt patching file 'template.txt' 

The patch command will automatically update and patch the file named template. You applied the patch to the template and that was it. You can now use cat or more and view the files you created to see what the differences are between them. Always remember to keep a copy of your template file around. In this example, the patch was applied directly to the template file and the template file was updated. If all further patches are created from the updated file, no problem; however, if patches are created based on the original template, then the template.txt can no longer be patched.

Now you should be comfortable with most of the common Unix file management techniques such as learning how to create, read, edit, move, and copy files. You should also feel comfortable using fancy little tricks such as reading the head or tail of a file, or using grep for its contents. We have covered a lot about file management and you should be very proud of yourself if you nailed it. These 10-minute lessons are meant to show you the basics, get you moving, and expose you to the rest.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net