Skip to content

Linux commands: diff

A quick guide to the `diff` command, used to compare files and directories

diff is a handy command. Suppose you have 2 files, which contain almost the same information, but you can’t find the difference between the two.

diff will process the files and will tell you what’s the difference.

Suppose you have 2 files: dogs.txt and moredogs.txt. The difference is that moredogs.txt contains one more dog name:

diff dogs.txt moredogs.txt will tell you the second file has one more line, line 3 with the line Vanille:

If you invert the order of the files, it will tell you that the second file is missing line 3, whose content is Vanille:

Using the -y option will compare the 2 files line by line:

The -u option however will be more familiar to you, because that’s the same used by the Git version control system to display differences between versions:

Comparing directories works in the same way. You must use the -r option to compare recursively (going into subdirectories):

In case you’re interested in which files differ, rather than the content, use the r and q options:

There are many more options you can explore in the man page running man diff:

The diff command works on Linux, macOS, WSL, and anywhere you have a UNIX environment


→ Get my Linux Command Line Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about cli: