Close Menu
    Facebook X (Twitter) Instagram
    • About
    • Privacy Policy
    • Write For Us
    • Newsletter
    • Contact
    Instagram
    About ChromebooksAbout Chromebooks
    • Linux
    • News
      • Stats
      • Reviews
    • AI
    • How to
      • DevOps
      • IP Address
    • Apps
    • Business
    • Q&A
      • Opinion
    • Gaming
      • Google Games
    • Blog
    • Podcast
    • Contact
    About ChromebooksAbout Chromebooks
    Linux

    How To Compare two files In Linux

    Dominic ReignsBy Dominic ReignsJanuary 9, 2026Updated:January 9, 2026No Comments4 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest

    Comparing files is essential for developers and system administrators who track code changes, debug issues, or verify configurations. Linux provides multiple command-line utilities for file comparison, with diff being the most widely used. This guide demonstrates practical methods to compare two files in Linux.

    Basic Syntax

    The diff command uses this syntax:

    diff [OPTION] FILES

    Common options include:

    • -s Report identical files
    • -c Display context mode output
    • -q Report only file differences
    • -y Show side-by-side comparison
    • -r Compare subdirectories recursively
    • -i Ignore case differences
    • -w Ignore whitespace
    • --ignore-file-name-case Ignore filename case
    • --no-ignore-file-name-case Consider filename case

    Create Files in Linux

    Create sample files for comparison testing.

    Create the first file:

    nano file1.txt

    Add this content:

    11 12 13 14 15 16 17 18 19
    eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
    Numbers in text and numeric format
    Sample data file

    Create the second file:

    nano file2.txt

    Add different content:

    10 12 3 14 8 16 7 18 19
    five twelve eight fourteen fifteen nine seventeen one nineteen
    Numbers in text and numeric format
    Sample data file

    Create a third file identical to the first:

    nano file3.txt

    Copy the same content from file1.txt:

    11 12 13 14 15 16 17 18 19
    eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
    Numbers in text and numeric format
    Sample data file
    Note: Use any text editor to create files. The nano editor is beginner-friendly and available on most systems.

    Print Differences Between Two Files

    Compare file1.txt and file2.txt:

    diff file1.txt file2.txt

    Output shows only differences:

    1,2c1,2
    < 11 12 13 14 15 16 17 18 19
    < eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
    ---
    > 10 12 3 14 8 16 7 18 19
    > five twelve eight fourteen fifteen nine seventeen one nineteen

    Display context mode with -c:

    diff -c file1.txt file2.txt

    Context mode includes surrounding lines:

    *** file1.txt	2024-01-08 10:00:00.000000000 +0000
    --- file2.txt	2024-01-08 10:00:30.000000000 +0000
    ***************
    *** 1,4 ****
    ! 11 12 13 14 15 16 17 18 19
    ! eveven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
      Numbers in text and numeric format
      Sample data file
    --- 1,4 ----
    ! 10 12 3 14 8 16 7 18 19
    ! five twelve eight fourteen fifteen nine seventeen one nineteen
      Numbers in text and numeric format
      Sample data file
    Tip: Context mode helps understand changes within file structure by showing unchanged lines around modifications.

    Find Similar Files Using Diff Command

    Check if two files match using -s:

    diff -s file1.txt file3.txt

    Output confirms identical files:

    Files file1.txt and file3.txt are identical

    This option verifies file integrity after copying or backup operations.

    Find Differ Files Using Diff Command

    Use -q to check if files differ:

    diff -q file1.txt file2.txt

    Output indicates differences:

    Files file1.txt and file2.txt differ

    This quick check avoids displaying detailed differences when you only need confirmation.

    Warning: Binary files require cmp command for accurate comparison. The diff command works best with text files.

    Display Output in Side-by-Side View

    Compare files side by side with -y:

    diff -y file1.txt file2.txt

    Side-by-side format shows both files:

    11 12 13 14 15 16 17 18 19             |  10 12 3 14 8 16 7 18 19
    eveven twelve thirteen fourteen        |  five twelve eight fourteen
    Numbers in text and numeric format        Numbers in text and numeric format
    Sample data file                          Sample data file

    Suppress identical lines using --suppress-common-lines:

    diff -y --suppress-common-lines file1.txt file2.txt

    Output displays only differences:

    11 12 13 14 15 16 17 18 19             |  10 12 3 14 8 16 7 18 19
    eveven twelve thirteen fourteen        |  five twelve eight fourteen
    Tip: Combine -y with -W to set custom column width for side-by-side comparison.

    Conclusion

    The diff command offers flexible options for comparing two files in Linux. Use basic comparison for quick checks, context mode for detailed analysis, or side-by-side view for visual comparison. Understanding these options helps automate file verification tasks and streamline development workflows.

    FAQs

    Use the diff command followed by two filenames. Run diff file1.txt file2.txt to display differences between files line by line.

    Lines starting with < represent the first file, lines with > represent the second file. Numbers indicate line positions and required changes to match files.

    Run diff -s file1.txt file2.txt to verify identical files. The command outputs “Files are identical” when content matches exactly.

    While diff works with binary files, use cmp command for byte-by-byte comparison. Run cmp file1 file2 for accurate binary file comparison.

    Add -w option to ignore all whitespace differences. Run diff -w file1.txt file2.txt to focus only on content changes, not formatting.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr
    Dominic Reigns
    • Website
    • Instagram

    As a senior analyst, I benchmark and review gadgets and PC components, including desktop processors, GPUs, monitors, and storage solutions on Aboutchromebooks.com. Outside of work, I enjoy skating and putting my culinary training to use by cooking for friends.

    Related Posts

    How to Restart NGINX on Your Linux Server

    January 30, 2026

    How To Install NPM Linux

    January 26, 2026

    How To Check Linux List Processes

    January 24, 2026

    Comments are closed.

    Best of AI

    Pephop AI Statistics And Trends 2026

    February 26, 2026

    Gramhir AI Statistics 2026

    February 24, 2026

    Poe AI Statistics 2026

    February 21, 2026

    Joyland AI Statistics And User Trends 2026

    February 21, 2026

    Figgs AI Statistics 2026

    February 19, 2026
    Trending Stats

    Chrome Incognito Mode Statistics 2026

    February 10, 2026

    Google Penalty Recovery Statistics 2026

    January 30, 2026

    Search engine operators Statistics 2026

    January 29, 2026

    Most searched keywords on Google

    January 27, 2026

    Ahrefs Search Engine Statistics 2026

    January 19, 2026
    • About
    • Tech Guest Post
    • Contact
    • Privacy Policy
    • Sitemap
    © 2026 About Chrome Books. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.