Skip to main content

The `patch` Command in Linux: Applying Changes with Precision

The patch command in Linux is a powerful tool used to apply changes to text files. It reads a diff (or patch) file containing a list of differences and applies those differences to one or more original files, effectively updating them to a new version. This utility is commonly used to apply updates or fixes to source code files distributed over the internet.

Syntax​

The basic syntax for the patch command is as follows:

patch [OPTIONS] [ORIGINALFILE [PATCHFILE]]
  • ORIGINALFILE is the file to be patched.
  • PATCHFILE is the file containing the differences.

If no file is specified, patch reads from the standard input.

Options​

Here is a table of some common options for the patch command:

OptionShorthandDescription
--backup-bMake a backup before applying the patch.
--directory-dChange to the specified directory before processing files.
--ignore-whitespace-lIgnore whitespace changes when matching lines.
--strip-pStrip the smallest prefix containing num leading slashes from file names.
--verbose-vProvide verbose output.
--reverse-RApply the patch in reverse.
--output-oOutput to the file instead of patching in place.
--dry-run-CDo not actually change any files; just print what would happen.
--silent-sWork silently unless an error occurs.
--helpDisplay a help message and exit.
--versionDisplay version information and exit.

Creating Example Files and a Diff​

Let's create an example file and a diff to demonstrate how patch works.

  1. Original File: hello.c

    vim hello.c

    Press i to insert text, then enter:

    #include <stdio.h>

    int main() {
    printf("Hello, world!\n");
    return 0;
    }

    Save and exit with :wq.

  2. Modified File: hello_modified.c

    vim hello_modified.c

    Insert the modified version of hello.c:

    #include <stdio.h>

    int main() {
    printf("Hello, everyone!\n");
    return 0;
    }

    Save and exit with :wq.

  3. Create a Diff File

    Use the diff command to create a patch file:

    diff -u hello.c hello_modified.c > hello.patch

Example 1: Applying a Patch​

Now we can apply the patch to hello.c:

patch hello.c hello.patch

The patch command will update hello.c with the changes defined in hello.patch.

Example 2: Applying a Patch in Reverse​

If you decide to revert to the original, you can apply the patch in reverse:

patch -R hello.c hello.patch

This will undo the changes made by hello.patch.

Example 3: Dry Run​

To check what changes patch would make without actually modifying any files:

patch --dry-run hello.c hello.patch

Example 4: Verbose Output​

For more detailed output during the patching process:

patch --verbose hello.c hello.patch

Example 5: Backup Original Files​

To make a backup of the original file before applying the patch:

patch --backup hello.c hello.patch

Combining patch with Other Commands​

You can combine patch with commands like cat if your patch is compressed or you receive it via standard input:

cat hello.patch | patch -p0

Here, -p0 means not to strip any leading directories from the file names in the patch file.

Conclusion​

The patch command is essential when dealing with modifications in text files, especially in a development environment where changes need to be tracked and applied systematically. By understanding the patch command and its options, Linux users can easily manage updates to their files, ensuring a smooth workflow when incorporating new changes or fixes.

What Can You Do Next 🙏😊​

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle