Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I compare two text files from the command line without editing either one?

Quick answer

If GNU Diffutils is available, run diff first.txt second.txt to compare the files line by line. The command reports differing lines; it is a comparison, not an instruction to edit either input. Read additions and removals relative to the order of the two files, and check the utility’s format and options before interpreting its output. For a first check, avoid options that ignore differences.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s a simple command I can use to check two text files without changing them?

Geez

If GNU Diffutils is available, start with:

diff first.txt second.txt

Replace the example names with the paths to your files. GNU diff compares files by lines and reports differences.[S1] This command is for comparing the files, rather than requesting edits to them. Keep the first comparison simple, and avoid sending the command’s output back into either input file. If the command isn’t available, your platform may have a different comparison utility, with different output conventions.

Follow-up question

How do I read what it reports? I want to tell which lines were added or removed.

Geez

Read the result relative to the order of the files: the first file is your reference point, and you’re checking what differs in the second. A line present in the second file but not the first is an addition relative to the first; a line present in the first but not the second is a removal relative to the second. GNU diff groups differing lines and can present them in several formats, so the exact layout depends on the format in use.[S1]

For example, suppose the first file contains Color: blue and the second contains Color: green. In everyday terms, that line changed. A difference report may present the old and new versions as a removal and an addition rather than as one specially labeled “changed” line. Check which file is first before interpreting the reported versions.

Follow-up question

Can I make sure it’s a fair comparison? I’m worried about spaces or capitalization.

Geez

For a first check, compare without options that ignore differences. GNU diff has options that can ignore whitespace, case, or selected lines, which can hide differences from the report.[S1] If you later use one of those options, remember that the result answers a narrower question: it doesn’t establish that the files match in every respect.

Also confirm that you have the intended file paths and order. If the filenames are similar, use paths that clearly identify the two files so you can tell which one is the reference and which one is being compared against it.

Follow-up question

What if I’m on Windows, or the command’s output looks different from an example I found?

Geez

Identify the utility before interpreting its output. GNU diff is line-oriented and supports several output formats.[S1] Windows PowerShell 5.1 also provides Compare-Object, which compares sets of objects; Microsoft documents examples using text-file contents. Its output uses <= and => for values found only in the reference or difference set, and == when equal items are included. Those are PowerShell-specific conventions, not GNU diff markers.[S2]

Use the documentation for the exact utility and options you ran. Don’t assume that another platform’s symbols or layout have the same meaning. If your goal is a straightforward line comparison, choose the available comparison tool and check its help or documentation for how it reports differences.

A practical checklist

  • Confirm the two paths point to the files you intend to compare.

  • Start with a plain comparison, such as diff first.txt second.txt when GNU Diffutils is available.

  • Keep the comparison output separate from the input files.

  • Interpret additions and removals relative to the order of the files.

  • Check the utility and its options before interpreting the output format.

  • Use options that ignore whitespace, case, or selected lines only when you deliberately want those differences excluded.

A few more useful answers

Does a changed line always appear as one “changed” item?

Not necessarily. A line-oriented comparison may present the old and new versions as a removal and an addition; the exact display depends on the format.[S1]

Can I trust a report that ignores spaces or capitalization?

It can answer a narrower comparison question, but it won’t show differences that the selected options ignore. For a first check, compare without those options.[S1]

Does PowerShell show differences the same way as GNU `diff`?

No. PowerShell 5.1 Compare-Object has its own object-comparison output conventions. Interpret its indicators using PowerShell documentation, not GNU diff examples.[S1][S2]

Sources

References used for this guide.

  1. [S1] GNU Diffutils: What comparison means
    gnu.org · Checked
  2. [S2] Microsoft Learn: Compare-Object (PowerShell 5.1)
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work