Compare Two Linux Config Files with diff in 5 Steps

To compare two Linux config files, run diff -u yesterday.conf today.conf with the older copy first. A line starting with a single minus sign came from the older file. A line starting with a single plus sign came from the newer one. Read the pair together to see what changed.

Two configuration printouts with an orange mark identifying the final line.

Suppose a scheduled job used to leave a CSV in /srv/reports. This morning the directory is empty, yet the job reports success. You have two saved copies of its configuration. Can they explain where the report went? The example below takes you from that question to a finding you could hand to the next person on call.

1. Make two small files you can compare

Use a new temporary directory so the example files stay separate from live settings. The commands below work in a POSIX-style shell on Linux. Run the remaining examples in the same shell and directory.

workdir=$(mktemp -d) && cd "$workdir" || exit 1
printf '%s\n' '[report]' 'directory=/srv/reports' 'format=csv' > yesterday.conf
printf '%s\n' '[report]' 'directory=/tmp/reports' 'format=csv' > today.conf

Each file has three lines. The section name and output format match. Only the directory differs. When you compare two Linux config files on a real system, choose a known-good copy and the candidate from the same host or deployment. Two files named production.conf can describe different jobs.

Compare two Linux config files with diff by placing the known-good copy first and the candidate second

Choose a baseline that answers the question

Before you compare two Linux config files from production, decide what the result should establish. A release template can answer “did someone customize this host?” A saved copy from before the incident can answer “what changed since the last successful run?” These questions can produce different answers on a perfectly healthy system. Record which one you are investigating.

Consider a template that has always used /srv/reports and a deployment that has always overridden it with /data/nightly. A template-to-host diff will show a difference every day. If yesterday’s job completed successfully with the override, its presence today does little to explain the new failure. The previous working deployment is a more informative reference point.

If you have only one copy, say so in the ticket. A dated package artifact, a configuration export or an approved revision in version control might supply the missing baseline. Check where it came from before calling it the previous running configuration. A filename containing “backup” tells you very little about who created it or whether the job ever used it.

2. Compare two Linux config files in a consistent order

diff -u -L yesterday.conf -L today.conf yesterday.conf today.conf

The final two arguments are the input files. The two -L options label the output so yours matches the example below. For everyday use, diff -u yesterday.conf today.conf works without the labels and normally includes modification times in the headers.

--- yesterday.conf
+++ today.conf
@@ -1,3 +1,3 @@
 [report]
-directory=/srv/reports
+directory=/tmp/reports
 format=csv

Read from the top. The three minus signs identify the old file and the three plus signs identify the new file. The @@ line describes the displayed range. On each side it starts at line 1 and spans three lines, including the surrounding context. Only the directory setting changed.

Notice the space before format=csv. It marks an unchanged line in this output. The job still asks for CSV in both copies. If the complaint concerns a missing PDF, this change alone does little to explain it. For the meaning of each marker, see the GNU guide to unified diff output.

Follow a second change through the same output

What happens when you compare two Linux config files containing more than one changed setting? Create a third file so both original samples remain available. This version sends reports to the temporary directory and asks the exporter to write JSON. Predict which unchanged line will disappear from the context before running the comparison.

printf '%s\n' '[report]' 'directory=/tmp/reports' 'format=json' > candidate.conf
diff -u -L baseline -L candidate yesterday.conf candidate.conf

You should now see two consecutive minus lines and two consecutive plus lines. The format setting was previously a context line because its value matched in both files. It now belongs to the changed block. Pair the old and new lines by key name. A unified diff groups a run of removed lines together and then prints the added lines. Each removed line need not be followed immediately by its replacement.

When you compare two Linux config files during a review, write one observation for each setting. Here the directory change could explain a missing file at the old path. The format change could explain why a consumer reading the new output rejects it. Keeping the two observations separate gives the next investigator two specific checks.

Which file did the change come from?

Suppose yesterday.conf specifies copies=1 and today.conf specifies copies=2. With yesterday first, the line beginning -copies=1 belongs to yesterday and +copies=2 belongs to today. Swap the filenames and both signs reverse, even though neither file has changed.

When you paste the result into a maintenance ticket, keep the two filename headers. A useful review note reads, “Proposed value: two copies. Previous value: one. Running job still to be checked.” That last sentence keeps the file comparison separate from the runtime observation.

3. Check the exit status before running another command

echo "$?"

For the example above, expect 1. The comparison finished and found a difference. Run the status check immediately after diff because the next command replaces the status you wanted to inspect.

Try all three outcomes. Keep these commands in order. The first comparison uses the same file twice. The last names a file that does not exist in the new example directory.

diff -q today.conf today.conf
echo "$?"
diff -q yesterday.conf today.conf
echo "$?"
diff -q yesterday.conf missing.conf
echo "$?"

With GNU diff, expect 0 for equal input, 1 for different input, and 2 for trouble, along with a diagnostic for the missing file. When a scheduled check reports a failure, capture the diagnostic as well as the number. An unreadable file leaves the comparison unresolved.

Use the exit status in a scheduled check. If a monitoring job treats every nonzero status as an error, an ordinary edit can look like a broken comparison. Capture the status in the branch that runs immediately after diff. The example below prints a short classification and leaves any diagnostic on the terminal.

if diff -q yesterday.conf today.conf >/dev/null; then
  echo "Same contents"
else
  status=$?
  case "$status" in
    1) echo "Contents changed" ;;
    *) echo "Comparison incomplete" >&2; exit "$status" ;;
  esac
fi
GNU diff exit codes: 0 for equal, 1 for different, 2 for trouble

Save the result in a shell script. If your next step is to write a log message, save the exit status before printing that message. Otherwise a successful logging command can overwrite the evidence that the files differed. This example handles an ordinary difference separately from a file access error. It works even in a script that has enabled set -e, because the comparison is the condition of an if statement.

if diff -q yesterday.conf today.conf; then
  result=0
else
  result=$?
fi
case "$result" in
  0) printf '%s\n' 'Same contents' ;;
  1) printf '%s\n' 'Review the changed lines' ;;
  *) printf '%s\n' 'Comparison could not finish' >&2 ;;
esac

To compare two Linux config files in a deployment check, decide what each outcome means for that job. A deliberate change might be the reason you are deploying. A missing baseline is a different condition, and continuing as though the files matched would hide it. If another program needs the original status, explicitly return that status at the end of the script.

4. Decide whether a noisy difference matters

A long diff can hide one consequential edit. Begin with the original unified diff and locate the section containing the disputed setting. If a key moved between sections, an identical value can now apply to a different job. Compare the surrounding section names before dismissing a move as housekeeping.

diff -u -b yesterday.conf today.conf

Use this as a second view when spacing obscures the result. The -b option ignores changes in the amount of whitespace. It still shows the directory change in this example. For a format where indentation affects meaning, keep the unfiltered comparison alongside it. A quiet filtered view cannot establish that the application interprets both files identically.

A whitespace experiment you can repeat. Try a change small enough to inspect by eye. The first command below creates a separate sample with three spaces between two words. The second creates the same line with one space. The ordinary diff reports the edit. The whitespace-filtered comparison treats both runs of spaces as equivalent.

printf '%s\n' 'label=nightly   report' > wide.conf
printf '%s\n' 'label=nightly report' > narrow.conf
diff -u wide.conf narrow.conf
diff -u -b wide.conf narrow.conf
echo "$?"

Expect the final status to be 0 with GNU diff. Now imagine a reporting program that prints the label exactly as written. Its output could still change. The comparison option governs what the tool displays. Only the application can tell you whether those spaces affect its behavior. This is a useful test when you compare two Linux config files generated by different formatting tools.

When every line appears to have changed

Sometimes you compare two Linux config files and see every line marked as changed, even though the text looks identical. Files edited on different systems may use different line endings. A carriage return can be difficult to see in ordinary terminal output. Recreate the symptom in a new sample before deciding that someone rewrote the configuration.

printf 'mode=batch\nformat=csv\n' > unix.conf
printf 'mode=batch\r\nformat=csv\r\n' > crlf.conf
diff -q unix.conf crlf.conf
od -An -t x1 -c crlf.conf

In the byte view, look for 0d 0a at the end of each line in the second file. The Unix-style sample ends those lines with 0a. With GNU diff, diff -u --strip-trailing-cr unix.conf crlf.conf lets you test the line-ending explanation without rewriting either file. If other text also changed, that text can still appear in the filtered comparison.

Keep the original files intact while investigating. The useful finding is that the bytes differ in a specific way and the line-ending filter accounts for the displayed changes. Whether a particular parser accepts both versions is a separate question. Check the behavior of the program and version that read the file before making a production edit.

When only the final newline is missing

printf 'format=csv\n' > complete.conf
printf '%s' 'format=csv' > unterminated.conf
diff -u complete.conf unterminated.conf

Here both value strings are identical. The second file ends immediately after the letter v. The output includes a message about the missing newline. That message describes the input and is printed by diff itself. If you paste an excerpt into a ticket, keep that message next to the line it describes. Otherwise the reviewer sees a removal and addition that look indistinguishable.

These two small experiments give you a quick way to distinguish line-ending differences across an entire file from one missing newline at the end. The remedies and their relevance to a run can be different. In particular, changing a single final newline cannot explain why every preceding line differs in a byte-for-byte comparison.

5. Verify which configuration the job loaded

The diff supports a precise observation. The newer copy directs output to /tmp/reports. To connect that edit to the missing CSV, find the configuration path in the scheduled invocation or the startup log for the run in question. Then check the reported output path and whether a file from that run is there.

If the scheduler loaded a third file, repeat the comparison using that file and its own known-good copy. If the file on disk changed after the job started, check when the running process last loaded it. This is where a timestamp and a configuration path earn their place in an incident note.

Check the active configuration path, load time and output destination

A note worth handing over. “The saved copies change the report directory from /srv/reports to /tmp/reports. Both specify CSV. The scheduled run finished at 06:00, but its configuration path is still unconfirmed. Next check the invocation and look for the 06:00 output in the candidate directory.” Replace the illustrative time with evidence from your own run.

Before sharing a diff, review unchanged context lines as well as additions and removals for credentials or private values. To turn your observations into a reproducible ticket, use the Linux bug report template and worked example.

Three outcomes for the missing-report case

The new directory contains the expected report. Match its timestamp or run identifier to the scheduled invocation. A file with the right name could be left over from an earlier test. If the identifiers match, tell the downstream reader where this run wrote the output and identify the setting that controls the location.

The new directory exists but is empty. Read the job’s message for the step that actually writes the report. Some jobs can finish without producing a file when the input set is empty. In that situation a diff can explain a configured destination, while the input count or exporter log explains the empty directory. Record which part of that chain you have checked.

The job names a different configuration. Start a fresh comparison for the file it names. Keep the first result attached to the investigation with a brief note about why it was set aside. Otherwise another person may rediscover the first pair, see an attractive explanation and repeat the same investigation.

When equal files still behave differently

When you compare two Linux config files and get a zero exit status, you can narrow the investigation. It means the compared file contents match under the options you selected. Two matching files can still be read by different program versions, different service accounts or different invocations. Start with the command line and the named configuration path for each run. Then follow only the discrepancy you can observe.

File permissions are another independent clue. A comparison performed from your own shell can succeed while a scheduled process lacks access to one of the parent directories. Record the account used for the failing run and its diagnostic message. A successful comparison under your own account does not establish that the scheduled job could open either file.

Finally, record the scope of your conclusion. “The two unfiltered copies compare equal” is a complete and repeatable finding. “The deployments are identical” would also require evidence about their inputs, startup options, environment and software versions. The narrower statement lets a colleague rule out one cause without accidentally ruling out several untested ones.

What evidence would change the diagnosis? Suppose the job log names the expected configuration, but the CSV in the new directory is from yesterday. The old timestamp weakens the theory that this run simply wrote to a different location. Check the run identifier, output filename and completion message before reporting that the missing file was found. Conversely, a fresh output file, a matching run identifier and a matching destination make a much better handoff than a directory listing alone.

Does input order matter when I compare two Linux config files? Yes, especially when writing an incident note. Run the same lab with diff -u today.conf yesterday.conf. You will now see /tmp/reports on the minus line. Nothing on disk has moved. The reference point changed. In a ticket, write down both filenames in the order you compared them. A colleague should be able to tell whether a setting was introduced or removed without guessing what you meant by “the other file.”

Can I compare two Linux config files without displaying their contents? Run diff -q yesterday.conf today.conf. This is useful in a shared terminal session when you only need to know whether the files differ. Switch to a unified view in a private workspace to identify the changed key.

How do I compare two Linux config files with spaces in their names? Quote each path separately, for example diff -u "baseline copy.conf" "current copy.conf". Quoting the entire pair as one string gives the command a single filename.

Can I compare two Linux config files from separate machines? Work from approved local copies. Record each source host, path and collection time so you can distinguish a deployment change from a host-specific setting. Keep sensitive copies in a location with the same access restrictions as the originals.

Compare two Linux config files when the last setting looks identical

When you compare two Linux config files, a unified diff can show the same setting on both sides. Look immediately below the removed line for a clue. Try two disposable files in an empty working directory.

printf 'mode=fast' > old.conf
printf 'mode=fast\n' > new.conf
diff -u old.conf new.conf
printf 'exit=%s\n' "$?"

The removed line reads -mode=fast, followed by \ No newline at end of file. The added line reads +mode=fast. The marker belongs to the line immediately above it. Here the old file ended after the final t and the new one ends with a newline. Exit status 1 records that difference. To check the line-ending count independently, use the wc -l guide to inspecting the final byte. In a review note, record “final newline added” and reserve any claim about runtime behavior for a check with the application that reads the file.

Stay Updated with Kioptrix

Get practical guides, useful resources, and new articles delivered to your inbox.

No spam. Unsubscribe anytime. Read our Privacy Policy.