Linux command
patch 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Apply a patch
patch < [patchfile.patch]
Apply patch with strip level
patch -p1 < [patchfile.patch]
Apply patch to specific file
patch [file] < [patchfile.patch]
Apply patch in dry-run
patch --dry-run < [patchfile.patch]
Reverse a patch
patch -R < [patchfile.patch]
Create backup
patch -b < [patchfile.patch]
Apply patch
patch -d [/path/to/directory] -p1 < [patchfile.patch]
Force apply
patch -f < [patchfile.patch]
说明
patch applies changes described in a patch file to original files. Patch files are typically created by diff and contain instructions for adding, modifying, or removing lines. The tool reads the patch file and attempts to apply each hunk (section of changes) to the target file. If a hunk doesn't match exactly, patch tries to find a location where it can apply with fuzz (allowing some context lines to differ). patch is essential for software development, applying updates to source code, and managing code changes distributed as patches.
参数
- -p _num_
- Strip num leading path components.
- -R, --reverse
- Reverse the patch.
- -N, --forward
- Ignore already applied patches.
- -b, --backup
- Create backup files.
- -d _dir_, --directory= _dir_
- Change to directory first.
- --dry-run
- Don't modify files, show what would happen.
- -f, --force
- Assume user knows best, don't ask.
- -i _file_, --input= _file_
- Read patch from file.
- -o _file_, --output= _file_
- Output to file.
- -F _num_, --fuzz= _num_
- Set max fuzz factor.
- -l, --ignore-whitespace
- Ignore whitespace changes.
- --verbose
- Output debugging information.
- -V _method_, --version-control= _method_
- Backup versioning method (simple, numbered, existing).
- -E, --remove-empty-files
- Remove empty files after patching.
FAQ
What is the patch command used for?
patch applies changes described in a patch file to original files. Patch files are typically created by diff and contain instructions for adding, modifying, or removing lines. The tool reads the patch file and attempts to apply each hunk (section of changes) to the target file. If a hunk doesn't match exactly, patch tries to find a location where it can apply with fuzz (allowing some context lines to differ). patch is essential for software development, applying updates to source code, and managing code changes distributed as patches.
How do I run a basic patch example?
Run `patch < [patchfile.patch]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -p _num_ do in patch?
Strip num leading path components.