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.
The question, the details, the next step.
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.txtwhen 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]
Sources
References used for this guide.
- [S1] GNU Diffutils: What comparison means gnu.org · Checked
- [S2] Microsoft Learn: Compare-Object (PowerShell 5.1) learn.microsoft.com · Checked
