Linux Tutorial / Compare Files with diff and cmp

Use diff when you need to see which text lines changed and how, and cmp when you need to know whether two files are identical byte for byte. Both generally return exit status 1 when the files differ, distinct from status 2 for trouble running the comparison. This lesson makes two small files and examines both output and exit status.

Prepare two files to compare

The exercise assumes Bash and GNU Diffutils. If ~/linux-course-13-practice exists, choose another directory name. Stop if the initial mkdir fails; the later > commands can overwrite existing files.

mkdir ~/linux-course-13-practice
cd ~/linux-course-13-practice
pwd

Once you have confirmed the location, create the files. Only the second line differs: beta versus BETA.

printf 'alpha\nbeta\ngamma\n' > old.txt
printf 'alpha\nBETA\ngamma\n' > new.txt

Read changed lines with diff

diff compares lines. -u selects unified format, which includes nearby unchanged lines. GNU --label replaces the usual filename-and-time headers with stable labels for this example.

diff -u --label old --label new old.txt new.txt

The practice files produce:

--- old
+++ new
@@ -1,3 +1,3 @@
 alpha
-beta
+BETA
 gamma

--- marks the earlier input and +++ the later input. The @@ header identifies the line range. A line beginning with - comes from the first file, one beginning with + from the second, and one beginning with a space is unchanged. diff does not know which file you consider the real original: argument order sets the comparison direction.

Find the first byte difference with cmp

cmp compares bytes and normally reports the location of the first difference. Here that is the first letter of the second line, after alpha and its newline.

cmp old.txt new.txt

With GNU cmp in an English locale, the message can look like old.txt new.txt differ: byte 7, line 2. Wording may change with filenames and locale, but the point is that the first difference is on line two. cmp does not list every later difference.

For a silent equality check, add -s and immediately examine the exit status. Since these files differ, this prints 1. $? refers to the most recently completed command; placing another command between the two lines would change what it reports.

cmp -s old.txt new.txt
echo $?

Distinguish differences from errors

Exit status Usual meaning for diff and cmp What to check
0 No difference found Confirm you compared the intended inputs
1 Files differ Read diff output for changed text
2 Trouble, such as an unreadable file Path, permission, and error message

cmp is a good fit for byte equality; diff -u is better for understanding a text change. Different line endings or encodings can create differences even when text looks similar. For a binary file, a text diff may not be helpful, so start with cmp if exact identity is the question. GNU diff -r can compare directory trees, but first check the paths because that expands the scope of comparison.

Official documentation

The GNU Diffutils manual documents unified output, cmp, and the exit statuses of both commands.

More in This Category
Linux pacman Command: Manage Packages on Arch Linux

Linux pacman Command: Manage Packages on Arch Linux

Learn how to manage Packages on Arch Linux with the Linux pacman command, including practical examples, key options, and important precautions.

Linux rm Command: Remove Files and Directories Safely

Linux rm Command: Remove Files and Directories Safely

Learn how to remove files and directory trees with Linux rm, use interactive safeguards, limit recursive deletion, and avoid destructive path mistakes.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux Tutorial / File System Basics: Paths and Key Directories

Linux Tutorial / File System Basics: Paths and Key Directories

Understand the Linux directory tree, read absolute and relative paths, and learn what common directories such as /home, /etc, and /var are used for.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname command, including practical examples, key options, and important precautions.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.