# Linux commands: diff

> Learn how the Linux diff command compares two files or directories line by line, with -y for a side-by-side view and -u for the unified format Git uses.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-10-05 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-diff/

`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:

![Terminal showing cat dogs.txt with Roger and Syd, then cat moredogs.txt with Roger, Syd, and Vanille](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_08.55.18.png)

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

![Terminal output showing diff dogs.txt moredogs.txt with 2a3 > Vanille indicating line 3 was added](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_08.56.05.png)

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

![Terminal output showing diff moredogs.txt dogs.txt with 3d2 < Vanille indicating line 3 was deleted](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_08.56.10.png)

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

![Terminal showing diff -y dogs.txt moredogs.txt with side-by-side comparison showing Vanille only in right column](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_08.57.56.png)

The `-u` option however will be more familiar to you, because that's the same used by the [Git](https://flaviocopes.com/git/) version control system to display differences between versions:

![Terminal showing diff -u output with unified format displaying file headers, timestamps, and +Vanille addition](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_08.58.23.png)

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

![Terminal showing ls dir1 and dir2 both containing dogs.txt, then diff -u dir1 dir2 showing unified format comparison](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_09.01.07.png)

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

![Terminal showing diff -rq dir1 dir2 output: Files dir1/dogs.txt and dir2/dogs.txt differ](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_09.01.30.png)

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

![Terminal showing man diff page with command description, synopsis, and various options like ignore-case, ignore-file-name-case](https://flaviocopes.com/images/linux-command-diff/Screen_Shot_2020-09-07_at_09.02.32.png)

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

If you'd rather compare two texts in the browser, try my free [diff checker](https://flaviocopes.com/tools/diff-checker/).
