Unix command line tricks for Linux, Mac, and Windows: diff

in #technology3 years ago (edited)

See Unix command line tricks for Linux, Mac, and Windows: table of contents for an introduction to this series and links to all the entries.

Today, we'll look at diff, which shows the difference between two files. It's pretty simple and very useful.

For our example, we'll use two files: first, this sampledata.csv file exported as a CSV file from a spreadsheet program like Excel or LibreOffice Calc:

Employee Number,Family Name,Given Name,Hire Date,Phone Extension

We'll also use this newsampledata.csv file, which is similar to the file above:

Employee Number,Family Name,Given Name,Hire Date,Phone Extension

How similar? We'll let diff tell us with this command:

 diff sampledata.csv newsampledata.csv

It gives us this output:

> 1000,Alvarez,Catherine,10/12/2016,x0314
< 1002,Smith,John,03/16/2017,x7225
> 1002,Smith,Jack,03/16/2017,x7225

The greater-than sign (>) refers to lines in the first filename passed to diff and the less-than sign (<) refers to lines in the second file. The first difference that the output above describes is a line in the first file that's not in the second; a record about Catherine Alvarez.

The second difference it found is a line that is in both files, but different, and the --- is between the pair of lines: the line from the first file is about John Smith, and the line from the second is about Jack Smith. All the other data on those lines is the same.

As with all Unix commands, you can learn more about diff with the manual (man) command:

man diff

I've been using diff for years and was very surprised just now to learn from the man command how many command line options diff has. For example, -y tells it to display the two files side-by-side with the differences marked:


Make sure to take a quick tour of what man has to say about the ways you can control diff output.

Coin Marketplace

STEEM 0.16
TRX 0.03
JST 0.027
BTC 13031.35
ETH 406.22
USDT 1.00
SBD 0.99