Linux diff Command {Syntax, Options and Examples} (2024)

Introduction

The Linuxdiffcommand compares two files line by line and displays the differences. This command-line utility lists changes you must apply to make the files identical.

Learn more about the diff command, its syntax, options, and use-case examples.

Linux diff Command {Syntax, Options and Examples} (1)

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to the terminal.

diff Command Syntax

The syntax for the diff command is:

diff [option] file1 file2

diff works without any options but needs two file names to provide an output.

diff Options

Use options to modify the output and make it easier to understand. The following table presents common diff command options.

OptionDescription
-iIgnores case.
-uPrints output without any redundant context lines.
-cOutputs several lines of context around the lines that differ.
-a / --textViews files as text and compare them line-by-line.
-b / --ignore-space-changeIgnores white spaces when comparing files.
--binaryCompares and writes data in binary mode.
-d--minimalModifies the algorithm (for example, to find a smaller set of changes).
-e / --edMakes output a valid ed script.
-E / --ignore-tab-expansionIgnores tab extension when comparing files.
-l / --paginateRuns the output through pr to paginate it.
-N / --new-fileTreats a missing file as present but empty.
-q / --briefOutputs whether files differ without specifying details.
-s / --report-identical-filesOutputs when the files are identical.
-w / --ignore-all-spaceIgnores white space when comparing files.
--versionShows the current diff command versions.
--helpProvides additional info about the command.

diff Output Format

When working with diff, it is crucial to know how to interpret the output.

diff output modes include:

  • Default
  • Context
  • Unified

diff Default Output

The following are instructions for interpreting the default diff output:

  • Lines starting with <. Refers to the content in the first file.
  • Lines starting with >. Refers to the content in the second file.
  • Line numbers. Correspond to the first file.
  • A symbol. Symbols indicate how the first file needs to be edited to match the second file. The output displays:
    • a (add)
    • c (change)
    • d (delete)
  • Line numbers corresponding to the second file.

diff Context Output

The following are instructions for interpreting the context output format:

  • A line starting with ***. Provides timestamp and info about the first file.
  • A line starting with ___. Provides timestamp and info about the second file
  • ****************. A separator.
  • A symbol.
    • -(minus). Indicates the content to be deleted from the first file.
    • +(plus). Indicates the content to be added to the first file.
    • !(exclamation mark). Indicates the content to be changed to the corresponding line from the second file.

diff Unified Output

The unified diff output is a more compact version of the context output. The only differences between the two are:

  • Omitted context lines.
  • Line range indication. @@ line-ranges @@ is used to describe the line ranges.

diff Command Examples

To show how the diff command works, create two sample text files. Take the following steps:

1. Create a Linux file named example1.txt using the Nano text editor or a text editor of your choice.

sudo nano example1.txt

2. Once the file is created, add the following lines to it:

AppleOrangeBananaWatermelonChery
Linux diff Command {Syntax, Options and Examples} (2)

3. Save and exit the file. Hold Ctrl + X and confirm by pressing Y.

4. Create an example2.txt file with:

sudo nano example2.txt

5. Add the following content to the file:

OrangePeachAppleBananaMelonCherry
Linux diff Command {Syntax, Options and Examples} (3)

6. Save the changes and exit.

After creating these two files, you can start comparing them. The following text elaborates on different practical examples of the diff command.

Note: To display the contents without opening the file for editing, use the cat command.

Compare Two Files

With the two sample files ready, use the diff command to see how they differ and how to make them identical. Run the following:

diff example1.txt example2.txt 
Linux diff Command {Syntax, Options and Examples} (4)

The output lists instructions on how to modify the first file to have the same content as in example2.txt.

The following is the output explanation for our sample files:

  • 1d0. The first line (1) from the first file should be deleted (d). If not, it appears in line 0 in the second file.
  • < Apple. The content you need to delete (as referred to with 1d0).
  • 2a2,3. In line 2 of the first file, add (a) lines 2 and 3 (2,3) from the second file.
  • > Peach, > Apple. The content you need to add (as referred to with 2a2,3).
  • 4c5. The fourth line (4) from the first file should be changed (c) to the fifth line (5) from the second file.
  • < Watermelon. The content you need to change.
  • > Melon. What you need to change it to.

Note: Once you have the output instructions, you can use the patch command to save the output and apply the differences.

Print Output in Context Form

The context format (-c) is a diff command option that outputs several lines of context around the lines that differ. To display the difference between the files in the context form, use the command:

diff -c example1.txt example2.txt
Linux diff Command {Syntax, Options and Examples} (5)

The output includes:

  • The first two lines. They display the name and timestamp of both files.
  • ****************. Used as a separator.
  • Two information lines. Display information about the first and the second file, starting with *** and ---.
  • ***1,6 **** and ---1,7 ---- . Indicate the line range of the files.
  • Content of the files. The beginning of each line instructs how to modify example1.txt to make it the same as example2.txt. If the line starts with:
    • - (minus), it needs to be deleted from the first file.
    • + (plus), it needs to be added to the first file.
    • ! (exclamation mark), it needs to be changed to the corresponding line from the second file.

Therefore, in the example above, delete Apple from the first line, replace Watermelon with Melon in line four, and add Peach and Apple to lines two and three.

Display More Compact Output

The unified format (-u) is an option that displays output in a more compact way. Instead of elaborating on each change, changes are grouped together in a few lines of context.

Run the following command to test:

diff -u example1.txt example2.txt
Linux diff Command {Syntax, Options and Examples} (6)

The output includes:

  • Lines displaying information about the files. The first file info begins with ---, while lines indicating the second file start with +++.
  • The first two lines. Display the name and timestamp of both files.
  • @@ -1,5 +1,6 @@. Show the line range for both files.
  • The files' content. The lines also include symbols that instruct you how to modify example1.txt. to make it identical to example2.txt. The symbols are:
    • - (minus). The line needs to be deleted from the first file.
    • + (plus). The line needs to be added to the first file.
    • No symbol. The line remains the same.

In the example above, the output instructs that Apple and Watermelon should be removed, whereas Peach, Apple, and Melon should be added.

Ignore Case

By default, diff is case-sensitive. If you want it to ignore case, add the -i option to the command:

diff -i file1 file2

For example, create file1.txt with the following lines:

AppleOrangeBananaWatermelonCherry
Linux diff Command {Syntax, Options and Examples} (7)

Create file2.txt with the content:

AppleorangeBananawatermelonCherry
Linux diff Command {Syntax, Options and Examples} (8)

Running diff with no additional options prints the differences between files.

diff file1.txt file2.txt
Linux diff Command {Syntax, Options and Examples} (9)

However, if you add the -i option, there is no output, as the command doesn't detect any differences in case.

diff -i file1.txt file2.txt
Linux diff Command {Syntax, Options and Examples} (10)

Print Command Version

To check the version of diff running on your system, run the command:

diff --version
Linux diff Command {Syntax, Options and Examples} (11)

Display Help Message

To output diff usage instructions run:

diff --help
Linux diff Command {Syntax, Options and Examples} (12)

Conclusion

The diff command helps you compare files and instructs you on how to modify them. This article showed you how to interpret its instructions to make the compared files identical.

Next, learn how to change the color of diff output.

Linux diff Command {Syntax, Options and Examples} (2024)
Top Articles
Open a trading Cent Account | HFM
Node.js LTS vs Current: Which Version to Choose?
Menards Thermal Fuse
Koopa Wrapper 1 Point 0
Tmf Saul's Investing Discussions
855-392-7812
Identifont Upload
Booknet.com Contract Marriage 2
Voordelige mode in topkwaliteit shoppen
Horoscopes and Astrology by Yasmin Boland - Yahoo Lifestyle
Sportsman Warehouse Cda
Call of Duty: NEXT Event Intel, How to Watch, and Tune In Rewards
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
2013 Chevy Cruze Coolant Hose Diagram
Derpixon Kemono
Baseball-Reference Com
Housing Intranet Unt
Www.paystubportal.com/7-11 Login
Labor Gigs On Craigslist
What is Cyber Big Game Hunting? - CrowdStrike
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Mccain Agportal
Why Does Lawrence Jones Have Ptsd
Curver wasmanden kopen? | Lage prijs
Robert Deshawn Swonger Net Worth
Dwc Qme Database
11 Ways to Sell a Car on Craigslist - wikiHow
Hannaford Weekly Flyer Manchester Nh
Synergy Grand Rapids Public Schools
Yale College Confidential 2027
Firefly Festival Logan Iowa
Gunsmoke Tv Series Wiki
A Man Called Otto Showtimes Near Carolina Mall Cinema
2487872771
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
Ripsi Terzian Instagram
Bt33Nhn
Pillowtalk Podcast Interview Turns Into 3Some
The Boogeyman Showtimes Near Surf Cinemas
Snohomish Hairmasters
Stafford Rotoworld
Jail View Sumter
My Locker Ausd
Trivago Anaheim California
Mbfs Com Login
Dyi Urban Dictionary
What is a lifetime maximum benefit? | healthinsurance.org
Concentrix + Webhelp devient Concentrix
How To Connect To Rutgers Wifi
2121 Gateway Point
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6206

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.