How to compare two files — and why diff says every line is different

Here's a familiar situation. You have two .env files: yours and the one a teammate sent. You compare them, and diff prints:
1,2c1,2
< API_URL=x
< DEBUG=1
---
> API_URL=x
> DEBUG=1
The lines are the same. Letter for letter. Yet diff is sure everything changed. We'll get to why. But first — a proper order of comparison, from "are they the same at all" to "what exactly changed in the folder".
Step by step: from a quick answer to the details
-
Find out whether the files are the same at all.
cmp a.env b.envIf the files match,
cmpstays silent. If not, it names the first difference:byte 10, line 1. For big files it's the fastest option. To compare at a distance, when the files sit on different machines, people check checksums. -
See the differences in a readable form.
diff -u old.js new.jsThe
-uflag gives you the familiar minus-and-plus format you see in Git and in AI answers. Minus — the line was there, plus — the line appeared. How to read it fully, mysterious@@included, is in the article what is a diff. -
If one word changed in a long line — compare by words.
git diff --no-index --word-diff old.txt new.txtIt works even outside a Git repo. Instead of two long lines you get one:
hello {+there+} world. Handy for prose, prompts and one-line configs. -
If line order doesn't matter — sort before comparing.
diff <(sort users1.txt) <(sort users2.txt)Lists of emails, dependencies or keys often match in meaning but come in a different order. Without sorting,
diffshows a pile of false differences. The<(...)construct passes a command's output as if it were a file. -
Compare whole folders.
diff -rq project-old project-new-rwalks into subfolders,-qprints only names:Only in project-old: notes.mdandFiles .../config.js differ. Then you rundiff -uon the specific files. -
Prefer your eyes — open it in an editor.
code --diff old.js new.jsVS Code and Cursor show the two files side by side with highlights. For long edits it's the most comfortable option.
Why diff says every line is different
Back to the .env puzzle. X-ray the files:
cat -A b.env
If the lines end in ^M$ instead of $, there's your answer. One file was saved with Windows line endings (\r\n), the other with Linux ones (\n). The invisible \r sits in every line, so for diff they all differ.
How to fix it:
- Compare while ignoring it:
diff --strip-trailing-cr a.env b.env. We tested it: on our examplediffwent quiet right away. - Fix the file itself:
sed -i 's/\r$//' b.env. - If you hit this regularly — set your editor to LF and add a
.gitattributeswith* text=auto.
Two more "invisible" culprits:
- Trailing spaces and tabs vs spaces. The
diff -wflag ignores any whitespace differences. - A last line with no newline.
diffhonestly writes\ No newline at end of file. It's a warning, not an error. But because of it, the last line still counts as changed.
What you end up with
After these steps you've got a kit for any case:
- same or not —
cmp; - what changed —
diff -u; - one word changed —
git diff --no-index --word-diff; - lists in a different order —
diffwithsort; - whole folders —
diff -rq; - "everything differs" but it looks identical —
cat -Aand--strip-trailing-cr.
It's also a great way to check an AI's work. Asked an agent to rewrite a config? Save the old version and compare. You'll see every change it made, not just the ones it told you about. If you work in Git, it's even easier: git diff shows the agent's edits right away.
How is diff different from cmp?
cmp compares bytes and names the first difference — good for images and archives. diff compares lines of text and shows every change.
How do I compare two files on Windows?
PowerShell has Compare-Object, but it's easier to install Git: it brings diff and git diff --no-index along. Or just open both files in VS Code with code --diff.
What does exit code 1 from diff mean?
That the files differ. It's not an error: 0 — identical, 1 — there are differences, 2 — a real problem, like a missing file.
Want to read AI-made changes with confidence? KODiQ has short lessons on the terminal, Git and reviewing agents' code.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





