FactorPad
Faster Learning Tutorials

Linux diff command summary with examples

diff allows you to compare lines of text files.
  1. Purpose - Learn what diff is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with diff.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, October 19, 2016
Updated: July 31, 2018
In this tutorial, 34 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / diff command


The Linux diff command

Beginner

Learn to compare text files at the Linux command line.

Video Tutorial

Linux diff command summary with examples (3:46)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our thirty-fourth word, or command to memorize is diff from our category Text.

diff allows you to compare lines of text files.

Recall from previous videos, we've been exploring text editing and diff is another useful skill here.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The diff command has nearly 50 options, and the arguments are the files or directories of files you'd like to compare.

Like most commands, help is available with double-dash --help. The diff command has so many options because files often come with a variety of codes for ends of lines and field delimiters for example. The -y option allows you to visualize changes in two columns.

So why is diff an important command? Well, you might be in a situation where you need to validate edits, backups and progress on a project. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's start by copying video34.txt to video35.txt.

$ cp video34.txt video35.txt

Then edit video35.txt using nano. Recall, in video 16, we used the touch command to change the date from the 10th to the 9th, let's change that back to the 10th to make it accurate.

File video35.txt (before).

GNU nano 2.2.6 File: video35.txt Oct 4:video05.txt Oct 10:video11.txt Oct 9:video16.txt Oct 12:video19.txt Oct 12:video20.txt Oct 13:video21.txt Oct 13:video23.txt Oct 15:video24.txt Oct 15:video26.txt Oct 15:video27.txt Oct 17:video31.txt Oct 18:video32.txt Oct 18:video33.txt

Ctrl-x to close, y and Enter to save.

File video35.txt (after).

GNU nano 2.2.6 File: video35.txt Oct 4:video05.txt Oct 10:video11.txt Oct 10:video16.txt Oct 12:video19.txt Oct 12:video20.txt Oct 13:video21.txt Oct 13:video23.txt Oct 15:video24.txt Oct 15:video26.txt Oct 15:video27.txt Oct 17:video31.txt Oct 18:video32.txt Oct 18:video33.txt

Next, let's do a diff -y the input file video34.txt, the output file video35.txt.

$ diff -y video34.txt video35.txt Oct 4:video05.txt Oct 4:video05.txt Oct 10:video11.txt Oct 10:video11.txt Oct 10:video16.txt | Oct 10:video16.txt Oct 12:video19.txt Oct 12:video19.txt Oct 12:video20.txt Oct 12:video20.txt Oct 13:video21.txt Oct 13:video21.txt Oct 13:video23.txt Oct 13:video23.txt Oct 15:video24.txt Oct 14:video24.txt Oct 15:video26.txt Oct 15:video25.txt Oct 15:video27.txt Oct 15:video27.txt Oct 17:video31.txt Oct 17:video31.txt Oct 18:video32.txt Oct 18:videi32.txt Oct 18:video33.txt Oct 18:video33.txt $ clear

Notice this mark here |. That's helpful.

And last, let's go optionless and interpret.

$ diff video34.txt video35.txt 3c3 < Oct 9:video16.txt --- > Oct 10:video16.txt

Think of the diff command as saying "what needs to be done to the input file to make it look like the output file?" There are three possible letters, a for add, c for change, and d for delete. So this means, change line 3 of the input file, to line 3 of the output file.

A Final Tip

Okay now you know how to use diff. And you know the syntax for commands, options and arguments.

One last tip about the diff command. So with nearly 50 options, see the capabilities in the manual page, and tune in for video 35.

Okay, thanks for visiting today. I hope this was a helpful introduction to the diff command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you like the videos as well, subscribe to the YouTube Channel here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the cut command, hit Back.
  • For another file comparison tool called comm, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / diff command



 
 
Keywords:
linux diff command
diff linux
linux compare files
unix diff
linux compare text
linux monitor changes to files
diff tool
compare directories
diff files
diff command
linux compare two files
diff directories
diff command syntax
linux diff tutorial
text diff
file diff
linux diff examples
man diff