Home » Linux » Shell » Linux Diff

Linux diff – How to Show Differences and Make Patches, With Examples

The diff command is an easy way to compare files or directories from the Linux shell. This article will show you how to use it, with some examples of common usage.

The diff command performs a line-by-line comparison of two files or directories and outputs the differences between them.

Why would you want to compare files or directories?

  • You might have two files with the same name that look similar and want to see the difference between them.
  • Comparing changes to the programming code in a project you’re building between a new and old version.
  • You’ve copied many files to another drive and want to make sure everything is copied successfully and without corruption.
  • You and a friend both have the same set of files in a folder. You have made changes to yours, but don’t want to send all of them to your friend again. You can use diff to create a patch containing only the changes made to those files, send it to them, and then bring their copy up to date without having to re-download everything.

You’ll probably have your own reasons for comparing files, so on to diff and how to use it.

diff Syntax


Note that:

  • OPTIONS are optional and can be supplied from the below table
  • FILES is the path to two files to be compared, separated by spaces

diff Options

Here are the most commonly used OPTIONS for the diff command, straight from the user manual:

-q, –brief Report only when files differ
-s, –report-identical-files Report when two files are the same
-u, -U NUM, –unified[=NUM] output NUM (default 3) lines of unified context
-N –new-file Treat absent files as empty
-l, –paginate Pass output through ‘pr’ to paginate it
-r, –recursive Recursively compare any subdirectories found
–no-dereference Don’t follow symbolic links
-a, –text Treat all files as text
-d, –minimal Try hard to find a smaller set of changes
–speed-large-files Assume large files and many scattered small changes

You can view the full manual here or run:

man diff

…from the command line for a full set of options.


Comparing Files with diff in Linux

This is the simplest usage of the diff command and will output the difference between the two supplied files to the console:

diff file1.txt file2.txt

Redirecting the Output

If you are simply looking to view the differences rather than creating a patch, simply redirecting the output to a file is useful – so that the terminal doesn’t cut off any changes if there are a lot of them:

diff -q file1.txt file2.txt > differences.txt

This will create (or overwrite!differences.txt, which will summarize the differences between the two files.

The -q options has been added so that only the differences are listed; files that are the same won’t be mentioned to make it easier to see what’s happening.

Comparing Directories with diff

Directories can also be compared using the -r (recursive) option to compare their contents:

diff -r -q /path/to/dir1 /path/to/dir2

Again, I have used the -q option here so that only differences are listed.

Comparing Large Files or Directories Containing Large Files

diff is an intensive process – every file being compared needs to be read and compared. It can take a while.

The –speed-large-files option alters diff’s algorithm to make it more efficient if you are comparing large files – speeding things up a bit.

Creating a Patch for a File

To create a patch – a list of differences between two files that can be applied to another copy of the first file to make it identical to the second, the following command can be used:

diff -u file1.txt file2.txt > update.patch

The -u option outputs the differences in a unified format which can be used by the patch command to update the original file with the changes.

This is useful if you have a friend or colleague who is using some of your data or programming code, and you want to send them updates you’ve made without sending over the whole dataset or application again.

Installing a Patch for a File

When your friend receives the patch, they can apply it using the following patch command:

patch original.txt < update.patch

Undoing a Patch for a File

If you made a mistake, you could undo a patch by running it in reverse with the -R option:

patch -R original.txt < update.patch

Creating a Patch for a Directory

The following command will create a patch for an entire directory by running the -r (recursive), -u (unified format), and -N (treat absent files as empty) options.

diff -ruN originalDir updatedDir > update.patch

Installing a Patch for a Directory

A patch can be applied to a directory using the -p0 option, which will apply the patch to the same directory structure as it was created in:

patch -p0 < update.patch

Undoing a Patch for a Directory

Finally, undoing/reversing the patching of a directory:

patch -R -p0 originalFile < patchFile


Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment