Ad space (banner)
🖥️Linux Lessons
Lesson 41 / 61

Comparing Two Files with diff

diff is for when "I want to compare where two files' contents differ." diff compares two given files line by line and shows only the parts that differ.

Use it in the form diff file1 file2. Adding the -u option (unified format) for a more readable diff display is common.

Running diff notes.txt sorted-fruits.txt compares the two files' contents and shows, with symbols, the lines that exist in only one, or that differ between the two.

A common early mistake is not understanding diff's output symbols (< and >) and misreading which file a line belongs to. < represents the first file, > the second.

In real work, this is used to compare a config file before and after a change, or to check a file diff in an environment where Git isn't available.

Linux
Try it (type this into the pseudo terminal)
diff notes.txt sorted-fruits.txt

🧑‍💻 You can type this command into the Linux pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)