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.
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
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
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 notdiff 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.
The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…
The free command in Linux gives you a quick summary of RAM and swap usage. It reads…
DNS cache is a temporary database your OS and browser build as you browse. Each time you visit a website, the domain-to-IP mapping is saved locally, cutting out the round trip to a remote DNS server on repeat visits. Stale entries are the most common reason to flush the DNS cache: a server moves to a new IP, but your OS or browser still routes to the old address. This guide covers how to clear the DNS cache on Windows, Linux, and macOS, and how to flush the separate cache stored inside Chrome and Firefox. Flush DNS Cache on Windows The command is the same on Windows 10, Windows 11, and earlier supported releases. Open Command Prompt with administrator privileges. Type cmd in the Windows search bar, right-click Command Prompt, and select Run as…
Using a strong, unique password for every account is one of the most effective security…
Ubuntu locks the root account by default. New users often wonder what the root password…
Since Ubuntu 17.10, networking is configured with Netplan rather than the older /etc/network/interfaces file. You write a…