Overview
Whenever you need to customize or substitute an upstream package binary without risking future APT upgrades wiping out your adjustments, dpkg-divert serves as the premier mechanism in Debian and Ubuntu systems. This walkthrough details the complete operational lifecycle—from orchestrating path relocations to safely unwinding them—guaranteeing that local scripts remain intact across package updates.
Inspecting Existing Diversions on Ubuntu
Executing dpkg-divert --list | head -n 10 reveals the first ten active file deflection entries maintained by the package manager.
Reviewing these records allows administrators to identify system-level file shunts, such as those established during usr-merge migrations or conflict-avoidance workflows.
⚙️ [Primary Parameters]
--list : Displays an inventory of active diversion rules across the OS.--add : Registers a fresh path redirection target in the system database.--remove : Erases an active redirection entry from the divert registry.--divert : Explicitly defines the alternate storage path where the displaced binary will reside.--local : Forces the diversion rule to apply uniformly across every package installation.--package : Declares a specific package exempt from the detour, granting it exclusive write privileges to the true path.--rename : Physically relocates the underlying binary on disk when registering or discarding a diversion.--test : Previews execution behavior without altering actual files or configuration databases.

Running dpkg-divert --list '*/man*' leverages wildcard pattern matching to isolate entries referencing manual page utilities.
The output demonstrates that the core /usr/bin/man executable has been systematically diverted to /usr/bin/man.REAL via a local policy rule.

Initializing Test Assets and Establishing a Path Diversion
To construct an isolated sandbox for testing, create an independent directory using mkdir -p /opt/divert_demo.

Populate a mock upstream binary by running echo 'echo Original script output' > /opt/divert_demo/service_tool to serve as our diversion candidate.

Grant the script execution rights using chmod +x /opt/divert_demo/service_tool so that it can be triggered directly from the terminal.

Verify baseline behavior prior to redirection by invoking /opt/divert_demo/service_tool and observing its console message.

Instruct the package management database to intercept this binary and rename it on disk using dpkg-divert --divert /opt/divert_demo/service_tool.orig --rename /opt/divert_demo/service_tool.
Upon invocation, the database registers a local rule redirecting traffic intended for /opt/divert_demo/service_tool to /opt/divert_demo/service_tool.orig.

Confirm the physical transformation on disk by executing ls -l /opt/divert_demo.
Thanks to the --rename flag, the original file has been automatically shifted to service_tool.orig within the filesystem.

Deploying Custom Wrappers and Validating Interception
Place your custom replacement binary in the canonical path using echo 'echo Custom wrapper executed' > /opt/divert_demo/service_tool.

Ensure the newly introduced wrapper script is runnable by running chmod +x /opt/divert_demo/service_tool.

Execute /opt/divert_demo/service_tool to verify that standard operational calls are now routed through your custom wrapper logic.
As expected, the terminal returns the custom notification string: Custom wrapper executed.

Next, validate that the preserved legacy binary remains completely operational by directly calling /opt/divert_demo/service_tool.orig.
The output yields the authentic Original script output, confirming that upstream code is protected rather than destroyed.

Query the exact status of the targeted resource using dpkg-divert --list /opt/divert_demo/service_tool.
The console confirms that a local diversion rule is persistently routing /opt/divert_demo/service_tool to /opt/divert_demo/service_tool.orig.

Reverting Diverted Paths and Restoring Original Binaries
Before unregistering the diversion, wipe out the wrapper script with rm -f /opt/divert_demo/service_tool.

To retire the database redirection rule and revert filesystem modifications in one stroke, issue dpkg-divert --rename --remove /opt/divert_demo/service_tool.
By incorporating --rename, the stored /opt/divert_demo/service_tool.orig archive is instantly restored to its standard /opt/divert_demo/service_tool location.

Inspect the working directory with ls -l /opt/divert_demo to audit the restoration.
Notice that the .orig artifact has disappeared, leaving only the restored service_tool script in place.

Confirm system fidelity by running /opt/divert_demo/service_tool once again.
The terminal prints the initial Original script output, demonstrating that native script execution is fully restored.

With our operational verification finished, remove the sandbox folder by issuing rm -rf /opt/divert_demo.

Leveraging dpkg-divert offers Debian and Ubuntu administrators a surgical, package-aware strategy for maintaining customized system environments without fighting package manager conflicts during automated maintenance cycles.