This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm , it tells us which lines in one file have is to be changed to make the two files identical. The important thing to remember is that diff uses certain special symbols and instructions that are required to make two files identical. It tells you the instructions on how to change the first file to make it match the second file.
Special symbols are: a : add c : change d : delete Syntax : diff [options] File1 File2 Lets say we have two files with names a. The first line of the diff output will contain: Line numbers corresponding to the first file, A special symbol and Line numbers corresponding to the second file. Like in our case, 0a1 which means after lines 0 at the very beginning of file you have to add Tamil Nadu to match the second file line number 1.
Next line contains 2,3c3 which means from line 2 to line 3 in the first file needs to be changed to match line number 3 in the second file. It then tells us those lines with the above symbols.
As a summary to make both the files identical, first add Tamil Nadu in the first file at very beginning to match line 1 of second file after that change line 2 and 3 of first file i.
Uttar Pradesh and Kolkata with line 3 of second file i. While this might not seem like an exciting thing to do, imagine being able to replicate the changes in a configuration file using a set of ed commands that allow you to just capture and replicate just the changes without any other effects. File one1 is now updated with the change commands needed to make it look like two. And you might be doing this on several hundred systems.
Or you could, of course, copy the changed file around. But making the changes as easy as possible and as schedulable as possible might just be the best approach. You might, after all, be sending the fixes to your customers or staff at remote locations. If you wanted, you could open the file to be changed in ed, type the commands 7c, eight, and so on as shown and the changes would be made. Then just exit ed with the w write command and quit with q.
To run the changes from the command line, you would do something like this:. The display of the file shows that the changes have been made. Another useful way to use the diff command is to use the —p option. The output is similar to above, but as you can see, the differences are "unified" into one set.
See the Examples section. The -e option tells diff to output a script, which can be used by the editing programs ed or ex , containing a sequence of commands. The commands are a combination of c change , a add , and d delete which, when executed by the editor, modify the contents of file1 the first file specified on the diff command line so that it matches the contents of file2 the second file specified.
We can run the following command to analyze the two files with diff and produce a script to create a file identical to file2. Notice that the changes are listed in reverse order: the changes closer to the end of the file are listed first, and changes closer to the beginning of the file are listed last.
This order is to preserve line numbering; if we made the changes at the beginning of the file first, that might change the line numbers later in the file. So the script starts at the end, and works backwards. Here, the script is telling the editing program: " c hange line 5 to the following line , and change lines 2 through 3 to the following two lines. Next, we should save the script to a file. This command does not display anything on the screen unless there is an error ; instead, the output is redirected to the file my-ed-script.
If my-ed-script. If we now check the contents of my-ed-script. There's still one thing missing, though: we need the script to tell ed to actually write the file. All that's missing from the script is the w command, which writes the changes. It redirects output to a file, but instead of overwriting the destination file, it appends to the end of the file. The command looks like this:. Now, we can check to see that our script has changed by running the cat command again:.
There are a lot of choices at your disposal when you wanto to compare files on Unix systems. Hopefully you'll find several that you really like using. Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" Unix as a second language but remembers enough English to write books and buy groceries.
She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders. Here are the latest Insider stories. More Insider Sign Out. Sign In Register. Sign Out Sign In Register. Latest Insider. Check out the latest Insider stories here. More from the IDG Network. Smart ways to compare files on Linux.
0コメント