Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Unix for Mac OS X Users unlocks the powerful capabilities of Unix that underlie Mac OS X, teaching how to use command-line syntax to perform common tasks such as file management, data entry, and text manipulation. The course teaches Unix from the ground up, starting with the basics of the command line and graduating to powerful, advanced tools like grep, sed, and xargs. The course shows how to enter commands in Terminal to create, move, copy, and delete files and folders; change file ownership and permissions; view and stop command and application processes; find and edit data within files; and use command-line shortcuts to speed up workflow. Exercise files accompany the course.
In this movie we're going to learn to control the format the DIFF uses for output. We can have an output that's something called copied context, which it would be c option. We can use the u option for unified context, and then we can have y for side-by-side comparison. There's a q option that will tell us only whether the two files are different. It won't report what that actual changes were to us. It'll just give us a single line saying yes, the two files are different. There are also a couple of other output formats but I think these are going to be the most common and the most useful for you. Let's take a look at them. So let me just clear my screen and let's do that same thing but now let's pass in that -c for copied context.
Now notice what it does is it gives us the whole first section, lines 1 through 11. It lets you know that here at the beginning. And here it is. That's everything that was in the first document. And then below that is everything that was in the second document. And it gives us a minus here letting us know that something got deleted and it gives us an exclamation point letting us know when something changed, and a plus down here letting us know when something was added. So this gives us essentially one set of changes and then right below it is another set of changes. Let's take a look at the y option next because that does essentially the same thing but it puts them side-by-side.
Now in my screen it can be widened a little bit but if you used a smaller font in Terminal you would be able to see them really side-by-side. What it does here is it shows us one file on the left, one file on the right, and then puts some symbols here in the middle to let us know if something was deleted, changed, or added. Now let's look at the u option. That's actually the one that I prefer. This gives us unified context. What that means is it essentially is smashing the two files together. What we're seeing is not what's in either file one or file two, but if we merged them together, you'll see that we have line 2 here with a minus next to it letting us know that this is something that's going to get deleted from the original file.
And down here we can see that the line is added, has a plus on it, and then the lines that have been changed here have a minus and a plus. The minus lets you know up here corresponds to original file while the plus corresponds to the revised file. Let's take a look at the q option so you could see what that does. If we compare the two files with q, it just comes up and says yes, these two files differ. That's it. it doesn't tell us what the changes are. Now let's just try it with two files that are the same such as the file compared with itself. Original file compared to original file.
Now we get no output back. That's because they are the same. If we wanted to report the fact that they're the same, we need to use that s option. And then it comes back and says yes, they are identical. Now let me just show you a few more things. Let's go back up and let's pick this -u option version comparing original file with revised file. What I want to do now is I want to output that into a file. We'll call it original_revised and the file extension that want to use would be diff. That's the standard for things which are an output from DIFF. So now we have what are the changes between these two documents saved as a document.
Let me also just show you that in a text editor. I have TextMate installed and I can just use the pipe it into mate and that'll pop it open in TextMate. I just want to show you that when you view that diff file it's something like a text editor that offers code coloring. It may offer you this kind of coloring for each of the lines which can be really handy. So now I can really easily see this is what was deleted, the green line down here is what was added, and the two lines here side-by-side highlights the two changes for me. Last of all, I want to show you that you can take this -u version and instead of piping it into my text editor, I am going to pipe it into another Unix tool called diffstat.
And it has to be the -u option for this to work. Doing the default one won't work. But if we pass -u into diffstat, it will come back and it'll tell you what the changes look like. Just a summary of those changes. Now a change in a file, neither delete or an append, but a change will be described as both an insertion and a deletion. So it lets you know here that it basically has two pluses and two minuses in this file. Four lines changed altogether. If we were looking at a comparison of directories, well, then we'd have a whole list of these. That might help you to very quickly identify where the bulk of the changes occurred between the two.
Now at the risk of stating the obvious, let me say that of course DIFF works best on files that are similar, such as our example where we have an original file and a file that's been modified. It's not very useful to compare two things that are really different. Let's say the Declaration of Independence with the Magna Carta. Well, then it would give you back every line as a difference. That's not useful. Of course, they're different on every single line. But if we're really trying to just track the changes where we know that something is similar but has a few differences, we really want to just identify those differences, then DIFF becomes an indispensable tool.
Find answers to the most frequently asked questions about Unix for Mac OS X Users.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.