LFCS · Question #844
If the current directory is /root and the kernel source is located in /usr/src/linux, which of the following commands should be used to apply the patch /tmp/foopatch?
The correct answer is D. cd /usr/src/linux; patch -p1 < /tmp/foopatch. To apply a patch, navigate to the target directory and use the patch command, specifying the patch level and redirecting the patch file as input.
Question
Options
- Acd /usr/src/linux; cat /tmp/foopatch | patch
- Bcd /usr/src/linux; cat /tmp/foopatch | patch -p0
- Ccd /usr/src/linux; patch -p1 > /tmp/foopatch
- Dcd /usr/src/linux; patch -p1 < /tmp/foopatch
- Ecat /tmp/foopatch | patch -p0
How the community answered
(60 responses)- A3% (2)
- B10% (6)
- C2% (1)
- D83% (50)
- E2% (1)
Why each option
To apply a patch, navigate to the target directory and use the `patch` command, specifying the patch level and redirecting the patch file as input.
`patch` without `-p` might fail due to incorrect path matching, and piping `cat` output is less direct than input redirection.
`patch -p0` would expect file paths in the patch file to be relative to the current directory *without* stripping any directory components, which is less common for kernel patches.
`patch -p1 > /tmp/foopatch` would attempt to write the *output* of the `patch` command to `/tmp/foopatch`, not apply the patch from it.
D is correct because `cd /usr/src/linux` changes to the directory where the patch needs to be applied, and `patch -p1 < /tmp/foopatch` then applies the patch. The `-p1` option tells `patch` to strip one leading directory component from the file paths in the patch file, which is common for patches generated from a `diff -uprN` command, and `< /tmp/foopatch` redirects the patch file content as input to the `patch` command.
Applying the patch from `/root` without changing directory would likely result in "File not found" errors for the paths within the patch.
Concept tested: Applying patches (`patch` command)
Source: https://man7.org/linux/man-pages/man1/patch.1.html
Topics
Community Discussion
No community discussion yet for this question.