FactorPad
Build a Better Process

Linux diff Command Summary with Examples

Linux 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.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux diff, 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 Tip Next

/ factorpad.com / tech / linux-essentials / diff-command.html


An ad-free and cookie-free website.


Examples of the Linux diff Command

Beginner

Learn to compare text files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux diff Command Summary with Examples (3:46)

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.

Common Linux diff Options
-options description
--help Print help screen
-y, --side-by-side Show output in two columns

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?

Three ways to stay in touch, 1) subscribe on YouTube, 2) follow @factorpad on Twitter and 3) join our no-spam email list.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / diff-command.html


linux diff command
diff linux
linux diff unix diff
diff unix
linux diff examples
diff tool
diff files
diff command
linux compare two files
diff directories
diff command syntax
linux compare two files
diff summary
linux diff options
linux diff tutorial
linux compare files
text diff
file diff
man diff
compare text files
diff compare

A newly-updated free resource. Connect and refer a friend today.