Where to start First steps track · 15 stages
Linux Labs

Compare two texts

The difference between two texts or configs by line and by word, as a diff -u patch.

Compare
Layout
Difference

The texts are identical.


          


Paste two texts or pick an example

Calculated in your browser — nothing is sent to the server

Practice on a machine

Lab “Change an address everywhere”

medium ~25 min stage 2 of the “Bash: text processing and scripts” track

Open the lab

In a Linux terminal

bash
cp nginx.conf nginx.conf.bak # a copy before editing diff -u nginx.conf.bak nginx.conf # the difference as a patch diff -w a.conf b.conf # ignore whitespace diff -rq /etc/nginx /backup/nginx # which files differ between directories git diff --no-index --word-diff a.txt b.txt # by word, even outside a repository

Keep in mindTrailing spaces and Windows line endings (CRLF) count as changes too, even when the lines look the same.

How to read diff -u

  • --- and +++ are the old and the new file.
  • @@ -1,4 +1,5 @@ is a hunk: 4 lines from line 1 in the old file, 5 in the new one.
  • - marks a removed line, + an added one, a space an unchanged one.

FAQ

Why are identical lines marked as changed?

They differ in something invisible: a trailing space, a tab, a \r\n line ending. If the mark goes away with “Ignore spaces”, whitespace is the cause.

How do I compare two files?

Paste their contents into “Before” and “After”. On a server, run diff -u file1 file2.

How do I apply the patch?

Save it to a file, fix the file names in the --- and +++ lines and run patch -p0 < file.patch. Try --dry-run first.

Loading…