LinuxCommandLibrary

patch

Apply differences between files (patching)

TLDR

Apply a patch using a diff file (filenames must be included in the diff file)

$ patch < [patch.diff]
copy

Apply a patch to a specific file
$ patch [path/to/file] < [patch.diff]
copy

Patch a file writing the result to a different file
$ patch [path/to/input_file] [[-o|--output]] [path/to/output_file] < [patch.diff]
copy

Apply a patch to the current directory
$ patch [[-p|--strip]] 1 < [patch.diff]
copy

Apply the reverse of a patch
$ patch [[-R|--reverse]] < [patch.diff]
copy

SYNOPSIS

patch [options] [originalfile] < patchfile

PARAMETERS

-pnum
    Strip num leading components from file names.

-i patchfile
    Read the patch from patchfile instead of standard input.

-o outfile
    Write output to outfile instead of the original file.

-R
    Reverse the patch (undo changes).

-E
    Remove empty output files after a successful patch.

-N
    Ignore patches that appear to be already applied.

-s
    Silent mode (only report errors).

-u
    Interpret the patch file as a unified diff.

-v
    Output version information.

-b
    Create backup files of the original files before patching.

-d directory
    Change to directory before performing actions.

DESCRIPTION

The patch command applies differences (patches) to files. These differences are usually created by the diff command. Patching allows you to update source code or other text files to a newer version without needing to transfer the entire file. The patch command reads a patch file, which describes the changes to be made, and then modifies the original files accordingly. It can handle various patch formats, including unified diffs and context diffs.

It intelligently attempts to deal with changes in line numbers and even attempts to find the original file if the location specified in the patch file is incorrect. This makes it a powerful tool for distributing updates and modifications, especially in collaborative software development environments. By default, patch creates backup files of the original files before applying the patch, which is helpful for reverting changes if needed.

CAVEATS

The patch command relies on accurate diff information in the patch file. Inaccurate or corrupted patch files can lead to incorrect or incomplete patching. Conflicts can occur when the original file has been modified since the patch was created. Always check for conflicts and resolve them before deploying patched code.

BACKUP FILES

By default, patch creates backup files with the .orig suffix. The -b option can customize backup behavior. It's crucial to manage these backups properly, especially in production environments.

FUZZ FACTOR

The patch command uses a "fuzz factor" to determine how much line number deviation is allowed when applying a patch. A higher fuzz factor allows for more flexibility but also increases the risk of incorrectly applying the patch.

HISTORY

The patch command originated in the early days of Unix and has been a staple tool for distributing source code updates. It was designed to address the need to share modifications to codebases efficiently, particularly in environments with limited bandwidth and storage. Over the years, it has evolved to support various diff formats and handle more complex scenarios, becoming a critical component of collaborative software development and system administration. Originally used in the context of software development, its adoption broadened to other forms of text based files as well.

SEE ALSO

diff(1), ed(1)

Copied to clipboard