patch

developmentLinux/Unix
The patch command is one of the most frequently used commands in Linux/Unix-like operating systems. patch Apply a diff file to an original

Quick Reference

Command Name:

patch

Category:

development

Platform:

Linux/Unix

Basic Usage:

patch [options] [arguments]

Common Use Cases

    Syntax

    patch [options] [originalfile [patchfile]]

    Options

    Option Description
    -b, --backup Make backup files
    -B PREFIX, --prefix=PREFIX Prefix for backup file names
    -d, --directory=DIR Change to directory DIR first
    --dry-run Print results without modifying any files
    -E, --remove-empty-files Remove output files that become empty
    -f, --force Force; assume patches for older versions
    -F NUM, --fuzz=NUM Set the fuzz factor to NUM (default=2)
    -i PATCHFILE, --input=PATCHFILE Read patch from PATCHFILE instead of stdin
    -l, --ignore-whitespace Ignore whitespace changes in context
    -n, --normal Interpret the patch as a normal diff
    -N, --forward Ignore patches that appear to be reversed or already applied
    -o FILE, --output=FILE Output patched files to FILE
    -p NUM, --strip=NUM Strip NUM leading components from file names
    -r REJECTFILE, --reject-file=REJECTFILE Output rejects to REJECTFILE instead of *.rej files
    -R, --reverse Assume patches were created with old and new files swapped
    -s, --quiet, --silent Work silently unless an error occurs
    -t, --batch Skip prompts about applying patches
    -T, --set-time Set times of patched files to time of context in patch file
    -u, --unified Interpret the patch as a unified diff
    -v, --version Print version information and exit
    -V METHOD, --version-control=METHOD Use METHOD version control (numbered, existing, simple)
    --verbose Output extra information about the work being done
    -x NUM, --context=NUM Set number of context lines copied from old file to NUM (default=3)
    -Z, --set-utc Set times of patched files to UTC
    --help Display help and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the patch command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    patch file.txt < changes.patch
    Apply changes from changes.patch to file.txt.
    patch < changes.patch
    Apply a patch automatically, letting patch determine the file to modify.
    # Advanced Examples Advanced
    patch -p1 < changes.patch Apply a patch with one directory level prefix stripped. patch --dry-run < changes.patch Show what would be done, but don't modify any files. patch -R file.txt < changes.patch Reverse a patch, undoing changes. patch -b file.txt < changes.patch Create a backup of the original file with a .orig extension. patch -p0 -i changes.patch Apply changes from changes.patch using -i flag to specify input file. diff -u original.txt modified.txt > changes.patch Create a patch file to use with the patch command. patch -o new_file.txt file.txt < changes.patch Apply changes to file.txt but write output to new_file.txt. patch --verbose < changes.patch Apply patch with detailed progress information.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The patch command is a powerful utility used to apply changes (or "patches") to text files. Patches are typically created by the diff command and contain instructions for transforming one file into another. This makes patch an essential tool for software development, source code management, and system administration. Originally developed by Larry Wall (the creator of Perl) in the 1980s, patch has become a cornerstone of open-source development, enabling contributors to share code modifications efficiently without exchanging entire files. It's particularly valuable in contexts where bandwidth or storage is limited, as patches are typically much smaller than the complete files they modify. Key features of the patch command: 1. Intelligent Patching: Patch can apply changes to files even if the original files have been modified slightly since the patch was created, thanks to its context-matching capabilities. 2. Multiple Formats: The command handles several patch formats, including normal diffs, context diffs, and unified diffs (the most common format today). 3. Directory Navigation: With the -p option, patch can strip path prefixes from filenames in the patch, allowing it to work correctly even when the current directory structure differs from that referenced in the patch file. 4. Backup Creation: Patch can automatically create backups of files before modifying them, allowing for easy recovery if problems occur. 5. Dry Run Mode: The --dry-run option allows users to see what changes would be made without actually modifying any files. 6. Reverse Application: With the -R option, patch can reverse the sense of the patch, effectively undoing changes rather than applying them. 7. Fuzzy Matching: The -F option controls the "fuzz factor" - how many lines of context can be ignored when finding the location to apply a patch. Common use cases for patch include: - Applying source code changes distributed by open-source projects - Implementing bug fixes or features from mailing lists or forums - Updating configuration files while preserving local modifications - Automating repetitive text file modifications - Collaborating on text documents with multiple contributors - Rolling back changes when needed - Applying changes across multiple files in a single operation While version control systems like Git have incorporated many of patch's capabilities, the command remains valuable for its simplicity, versatility, and universal availability across Unix-like operating systems. It's especially useful in situations where full version control is not in place or when working with systems where only minimal tools are available. The patch command works best with line-oriented text files and is less effective with binary files or files with very long lines. For those cases, specialized binary diff and patch tools are more appropriate. Nevertheless, for its intended purpose of managing changes to source code and other text-based content, patch remains an irreplaceable tool in the developer's toolkit.

    Related Commands

    These commands are frequently used alongside patch or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the patch command works in different scenarios.

    $ patch
    View All Commands