Cybersecurity Updates & Tools

diff Command in Linux: Compare Files and Create Patches

The diff command in Linux compares two text files line by line and shows the lines that differ. When files are identical, it produces no output. That behavior is useful in scripts — you can test the exit code without reading any output at all.

The most common practical use for diff is creating patch files. The differences captured by diff can be applied to another copy of the file using the patch command, which is how source code patches have been distributed for decades.

How the diff Command Works in Linux

The basic syntax is:

bashdiff [OPTIONS] file1 file2

To save the output as a patch file:

bashdiff -u file1 file2 > changes.patch

Exit codes make diff scriptable. It returns 0 when files are identical, 1 when they differ, and 2 if an error occurred. Use -q to suppress all output and only get the exit code:

bashif diff -q file1 file2 > /dev/null; then  echo "Files are identical"fi

Normal, Context, and Unified Output Formats

diff supports three main output formats. The unified format is the most widely used because it is compact and expected by version control tools including git.

Normal format (default, no option):

bashdiff file1 file2

Output consists of change commands like 2d2 or 4c4,5. Each has three parts: the line range in file1, a change type (a = add, c = change, d = delete), and the line range in file2. Lines starting with < come from file1; lines starting with > come from file2.

Context format (-c). Adds surrounding lines of context around each change, making it easier to locate the difference in a large file. Default context is three lines; use -C N to change it:

bashdiff -c file1 file2

Line markers: double space = unchanged context; - = missing from file2; + = missing from file1; ! = changed between files. Each ! block in file1 has a matching ! block in file2.

Unified format (-u). An improvement over context format. Context lines appear once instead of twice, making the output smaller and cleaner. This is the format git diff uses internally and what the patch command expects by default:

bashdiff -u file1 file2

Lines starting with - are removed from file1. Lines starting with + are added. Unchanged context lines start with a single space. The @@ header shows which line ranges are included in each section.

Ignore case with -i:

bashdiff -ui file1 file2

Compare Directories and Other Useful Options

To compare two directories recursively and see every differing line:

bashdiff -r dir1 dir2

For a quick summary that only reports which files differ, not what changed:

bashdiff -rq dir1 dir2

Whitespace handling matters when comparing code across different editors:

  • -w — ignores all whitespace. A line with four spaces and a line with a tab are treated as identical
  • -b — ignores changes in the amount of whitespace. Useful when indentation style has changed but structure has not

diff vs cmp: diff compares line by line and reports every difference. cmp compares byte by byte and stops at the first difference. Use cmp when you only need to know whether files differ; use diff when you need to see what changed.

Use diff -u as your default. It produces the compact unified format that git, patch, and most code review tools expect. Leave a comment below if you run into any issues.