patch

patch

The patch command applies a diff file (usually generated by the diff command) to one or more original files, resulting in a patched version of those files.

Basic Syntax

patch [options] [original-file [patchfile]]
  • original-file: The file(s) to which the patch should be applied.

  • patchfile: The file containing the patch to be applied.

Key Options

  • -pNUM, --strip=NUM: Strip the smallest prefix containing NUM leading slashes from filenames found in the patch file. This is useful when the paths in the patch file do not match exactly with the paths in your current directory structure.

  • -i PATCHFILE, --input=PATCHFILE: Specifies the patch file to use.

  • -R, --reverse: Reverses the patch, i.e., removes the changes applied by the patch from the original file(s).

  • -o OUTFILE, --output=OUTFILE: Specifies an output file for rejected hunks (parts of the patch that could not be applied cleanly).

  • -b, --backup: Make a backup before applying the patch.

Examples

  1. Applying a Patch

    Apply a patch to a file:

    patch -i patchfile.txt originalfile.txt
  2. Specifying Patch File

    Specify the patch file to use:

    patch -i mypatch.diff
  3. Reverse Patch

    Reverse a patch (unapply changes):

    patch -R -i mypatch.diff
  4. Handling Rejected Patches

    Save rejected hunks to a file:

    patch -o rejected.txt -i mypatch.diff
  5. Strip Leading Directory

    Strip the leading directory from filenames in the patch file:

    patch -p1 -i mypatch.diff

Explanation with Examples

  1. Applying a Patch

    Applying a patch to a file involves using the -i option followed by the patch file and specifying the original file:

    patch -i patchfile.txt originalfile.txt

    This command applies the changes specified in patchfile.txt to originalfile.txt.

  2. Specifying a Patch File

    If you have a patch file named mypatch.diff, you can apply it directly:

    patch -i mypatch.diff

    This command applies the changes specified in mypatch.diff to the corresponding files.

  3. Reversing a Patch

    To reverse or undo a patch, you use the -R option:

    patch -R -i mypatch.diff

    This command removes the changes applied by mypatch.diff from the original files.

  4. Handling Rejected Patches

    When a patch cannot be applied cleanly (due to conflicts), you can save the rejected parts to a file using the -o option:

    patch -o rejected.txt -i mypatch.diff

    This command saves the rejected hunks (parts of the patch that couldn't be applied) to rejected.txt.

  5. Stripping Leading Directory

    If your patch file contains paths with a different structure than your current directory, you can strip the leading directory component using the -p option:

    patch -p1 -i mypatch.diff

    This tells patch to ignore the first directory component in the paths specified in mypatch.diff.

Usage in Scripts and Automation

The patch command is often used in scripts and automated processes, especially in software development and system administration tasks where applying patches to files and directories is necessary for updates and fixes.

Conclusion

The patch command is essential for managing changes to files and directories in a controlled and reversible manner. It allows you to apply, reverse, and manage patches efficiently, making it a powerful tool for maintaining software and system configurations.

help

Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]

Input options:

  -p NUM  --strip=NUM  Strip NUM leading components from file names.
  -F LINES  --fuzz LINES  Set the fuzz factor to LINES for inexact matching.
  -l  --ignore-whitespace  Ignore white space changes between patch and input.

  -c  --context  Interpret the patch as a context difference.
  -e  --ed  Interpret the patch as an ed script.
  -n  --normal  Interpret the patch as a normal difference.
  -u  --unified  Interpret the patch as a unified difference.

  -N  --forward  Ignore patches that appear to be reversed or already applied.
  -R  --reverse  Assume patches were created with old and new files swapped.

  -i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.

Output options:

  -o FILE  --output=FILE  Output patched files to FILE.
  -r FILE  --reject-file=FILE  Output rejects to FILE.

  -D NAME  --ifdef=NAME  Make merged if-then-else output using NAME.
  --merge  Merge using conflict markers instead of creating reject files.
  -E  --remove-empty-files  Remove output files that are empty after patching.

  -Z  --set-utc  Set times of patched files, assuming diff uses UTC (GMT).
  -T  --set-time  Likewise, assuming local time.

  --quoting-style=WORD   output file names using quoting style WORD.
    Valid WORDs are: literal, shell, shell-always, c, escape.
    Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.

Backup and version control options:

  -b  --backup  Back up the original contents of each file.
  --backup-if-mismatch  Back up if the patch does not match exactly.
  --no-backup-if-mismatch  Back up mismatches only if otherwise requested.

  -V STYLE  --version-control=STYLE  Use STYLE version control.
        STYLE is either 'simple', 'numbered', or 'existing'.
  -B PREFIX  --prefix=PREFIX  Prepend PREFIX to backup file names.
  -Y PREFIX  --basename-prefix=PREFIX  Prepend PREFIX to backup file basenames.
  -z SUFFIX  --suffix=SUFFIX  Append SUFFIX to backup file names.

  -g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.

Miscellaneous options:

  -t  --batch  Ask no questions; skip bad-Prereq patches; assume reversed.
  -f  --force  Like -t, but ignore bad-Prereq patches, and assume unreversed.
  -s  --quiet  --silent  Work silently unless an error occurs.
  --verbose  Output extra information about the work being done.
  --dry-run  Do not actually change any files; just print what would happen.
  --posix  Conform to the POSIX standard.

  -d DIR  --directory=DIR  Change the working directory to DIR first.
  --reject-format=FORMAT  Create 'context' or 'unified' rejects.
  --binary  Read and write data in binary mode.
  --read-only=BEHAVIOR  How to handle read-only input files: 'ignore' that they
                        are read-only, 'warn' (default), or 'fail'.

  -v  --version  Output version info.
  --help  Output this help.

Last updated