117-201 · Question #306
What does the -p3 option to the patch command do?
The correct answer is A. It will strip off path information from each file mentioned in the patch file up to and including the. See the full explanation below for the reasoning.
Question
Options
- AIt will strip off path information from each file mentioned in the patch file up to and including the
- Bpatch continues execution as long as there are three or fewer errors.
- CIt instructs patch to look up to three lines of context before or after the declared line in the original
- Dpatch will keep three previous versions of each file in the output to prevent loss of change history.
- EIt instructs patch to conform more strictly to the POSIX standard.
How the community answered
(35 responses)- A83% (29)
- C3% (1)
- D9% (3)
- E6% (2)
Community Discussion
7The answer is A, and this is one worth understanding rather than just memorizing. The -p option in patch controls how many leading path components get stripped from the filenames listed in the patch file. So -p3 strips three levels of directory prefix, meaning a path like home/user/project/file.c becomes just file.c after the strip. This matters because patches are often generated on one machine with a different directory structure, and you need to tell patch how to map those paths to your local files. The other options are total distractors, since patch does not track version history, has no three-error tolerance behavior, and the POSIX conformance flag is something else entirely. Our study group had a small debate on whether the strip count includes or excludes the filename itself, and the consensus landed on it counting only the directory separators and their preceding components, not the filename. If anyone tested this differently, drop your findings below.
The strip count part is exactly right, but your example is off by one: "home/user/project/file.c" has three slashes, so -p3 strips "home/user/project/" and leaves "file.c", which means you need to count the components being removed, not the slashes, and your path actually has three components before the filename, so the example does work out, but it is easy to confuse yourself by counting slashes versus counting the segments between them.
Think of the number in -p as a "path peeler," like peeling layers off an onion, so -p3 peels three directory levels off the front of each filename listed in the diff header. Quick question for the group though: if your patch file shows a path like a/b/c/myfile.c, does -p3 leave you with just myfile.c, or does the count include the filename itself?
The count is just directory separators so -p3 on a/b/c/myfile.c strips a, b, and c and leaves you with myfile.c, but worth noting that the a/ and b/ prefixes git adds are purely convention so on a real git-generated patch you usually only need -p1.
Think of file paths in a patch like a full street address written out from country down to apartment number. If the patch was generated on someone else's machine with a deeper folder structure, the path embedded in that patch might say something like "home/devbox/projects/src/myfile.c" but on your machine the file just lives at "src/myfile.c". The -p option tells patch how many address levels to rip off the front before it even tries to find the file, so -p3 chops the first three directory segments off every path listed in the patch. This is why you will almost always see -p1 with git-generated diffs, since git prefixes paths with "a/" and "b/" and you need to strip exactly one level to land on the real file. Knowing that the number after -p is a count of path separators to remove, not a line count or an error tolerance, which answer above describes stripping path components rather than counting errors or context lines?
My sticky hook for this is that the p in -p stands for Peel, so picture yourself peeling onion skins off the front of the path one skin per slash, and the digit is just your peel count, which is why -p1 on a git diff neatly peels away that one decorative "a/" or "b/" skin and lands you right on the real file.
A is right. Took my exam last spring, saw -p3, almost picked C because "context" fooled me, but I remembered -p strips path components, not context lines.