How to Diff CSV Files on the Command Line
Why plain diff falls short for CSVs, and how to get a meaningful row-level comparison from the terminal or the browser.
Reaching for diff or git diff on two CSV files is tempting, but line-based diffs treat a CSV as text, so a single reordered row, a re-sorted file, or a changed column order shows up as a wall of changes that tells you nothing. Line diff has no concept of rows matched by a key.
For a useful command-line CSV diff you need a tool that parses the CSV and aligns rows by a key column, then reports added, removed, and changed records. Several exist (csvdiff, daff, and database-backed approaches), and when a one-off is faster than scripting, a browser-based diff does the same key-aware comparison without any setup.
- 1
Normalize both files first
Sort both files by the key column and ensure identical headers and encoding. This alone makes a plain diff far less noisy, though still line-based.
- 2
Use a CSV-aware diff tool
Tools like csvdiff or daff parse the columns and match rows by a key, reporting real cell-level changes. Install one and point it at both files with the key column specified.
- 3
Or pipe through a database
Load both files into a database and use an anti-join or EXCEPT query to find rows present in one but not the other, which scales to large files.
- 4
For a quick one-off, use the browser
When scripting isn't worth it, drop both files into SheetCompare for the same key-aware, cell-level diff with no install.
Frequently asked questions
Why doesn't git diff work well for CSVs?▾
It compares lines, not records. A re-sorted file or a reordered column produces a huge diff even when the data is identical, because every line moved.
What key should I diff on?▾
A column that uniquely identifies each row, an ID, an email, an order number. Without a stable key, a diff tool can't tell which row in file A corresponds to which in file B.
Can I automate this in CI?▾
Yes. CSV-aware diff tools have exit codes and machine-readable output suitable for scripts and pipelines.