When working with patch files on older CentOS systems, you might encounter the frustrating "command not found" error. This typically occurs because the patch
utility isn't installed by default on minimal CentOS installations.
First, let's check if the package exists in your repositories:
yum list available | grep patch
For CentOS 5.x, you should see output similar to:
patch.x86_64 2.5.4-29.2.2 base
There are two reliable ways to install the patch utility:
# Method 1: Basic installation
yum install patch
# Method 2: Install with development tools (recommended)
yum groupinstall "Development Tools"
If you're working in a restricted environment without root access:
# Option 1: Download and compile from source
wget https://ftp.gnu.org/gnu/patch/patch-2.7.6.tar.gz
tar -xzf patch-2.7.6.tar.gz
cd patch-2.7.6
./configure --prefix=$HOME/.local
make
make install
# Add to PATH
export PATH=$HOME/.local/bin:$PATH
After installation, verify it works:
patch --version
Expected output should show version information:
GNU patch 2.5.4
Let's walk through a complete example:
# Create original file
echo -e "line1\nline2\nline3" > original.txt
# Create diff file
echo -e "--- original.txt\t2023-01-01\n+++ modified.txt\t2023-01-01\n@@ -1,3 +1,3 @@\n line1\n-line2\n+new line2\n line3" > example.patch
# Apply patch
patch original.txt < example.patch
- If you get permission errors, try
sudo yum install patch
- For corporate environments, check with your admin about approved packages
- Consider using
git apply
if you only need to apply Git-formatted patches
While patch
might seem like a simple utility, it's crucial for many development workflows. Keeping your development tools properly installed will save you from similar issues in the future.
When working with source code modifications on older CentOS systems, you might encounter the frustrating "command not found" error when trying to apply patches. This typically means the patch
utility isn't installed on your system.
$ patch -p0 < foo.patch
bash: patch: command not found
For CentOS 5.x, you'll need to install the GNU patch package using yum:
$ sudo yum install patch
Loading "installonlyn" plugin
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package patch.x86_64 0:2.5.4-31.2.2 set to be updated
--> Finished Dependency Resolution
...
Complete!
After installation, verify it works by checking the version:
$ patch --version
GNU patch 2.5.4
Copyright (C) 1988 Larry Wall
When applying patches, these are the most common patterns you'll use:
# Standard patch application
$ patch -p1 < feature_update.patch
# Dry run to test without modifying files
$ patch --dry-run -p0 < security_fix.patch
# Reverse a previously applied patch
$ patch -R -p1 < bugfix_revert.patch
If you encounter problems after installation:
# Check if patch is in your PATH
$ which patch
/usr/bin/patch
# Verify file permissions
$ ls -l /usr/bin/patch
-rwxr-xr-x 1 root root 108K Mar 15 2010 /usr/bin/patch
If yum isn't available, you can:
# Download the RPM directly
$ wget http://vault.centos.org/5.11/os/x86_64/CentOS/patch-2.5.4-31.2.2.x86_64.rpm
$ sudo rpm -ivh patch-2.5.4-31.2.2.x86_64.rpm
# Or compile from source
$ curl -O https://ftp.gnu.org/gnu/patch/patch-2.7.6.tar.gz
$ tar xvf patch-2.7.6.tar.gz
$ cd patch-2.7.6
$ ./configure
$ make
$ sudo make install
The -p
flag determines how many directory levels to strip:
# Original path in patch file: a/src/main.c
$ patch -p0 < patchfile # looks for a/src/main.c
$ patch -p1 < patchfile # looks for src/main.c
$ patch -p2 < patchfile # looks for main.c
For completeness, here's how to generate patches:
$ diff -Naur original/ modified/ > changes.patch
$ diff -u old_file.c new_file.c > file_update.patch