Guides

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

Illustration: two dark stone tablets side by side, the same row highlighted on both, one with an extra block

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

  1. Find out whether the files are the same at all.

    cmp a.env b.env
    

    If the files match, cmp stays 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.

  2. See the differences in a readable form.

    diff -u old.js new.js
    

    The -u flag 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.

  3. If one word changed in a long line — compare by words.

    git diff --no-index --word-diff old.txt new.txt
    

    It 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.

  4. 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, diff shows a pile of false differences. The <(...) construct passes a command's output as if it were a file.

  5. Compare whole folders.

    diff -rq project-old project-new
    

    -r walks into subfolders, -q prints only names: Only in project-old: notes.md and Files .../config.js differ. Then you run diff -u on the specific files.

  6. Prefer your eyes — open it in an editor.

    code --diff old.js new.js
    

    VS 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 example diff went 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 .gitattributes with * text=auto.

Two more "invisible" culprits:

  • Trailing spaces and tabs vs spaces. The diff -w flag ignores any whitespace differences.
  • A last line with no newline. diff honestly 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 — diff with sort;
  • whole folders — diff -rq;
  • "everything differs" but it looks identical — cat -A and --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.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →