I have a patch with absolute paths that I wish to use. i.e. the first few lines are as follows.

--- /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml  2014-10-10 18:47:23.000000000 +1100
+++ /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml.mod  2014-11-11 09:44:17.786200477 +1100

However, it fails unless I am in the root directory.

~$ cd
~$ sudo patch -i /tmp/fix_kde_icons.patch -p0
Ignoring potentially dangerous file name /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml
Ignoring potentially dangerous file name /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml.mod
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
~$ cd /tmp
/tmp$ sudo patch -i /tmp/fix_kde_icons.patch -p0
... #same error as above
/tmp$ cd /usr
/usr$ sudo patch -i /tmp/fix_kde_icons.patch -p0
... #same error as above
/usr$ cd /
/$ sudo patch -i /tmp/fix_kde_icons.patch -p0
patching file /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml

Is there a way to make patch use the absolute path with any working directory?

  • I would edit the patch itself instead.
    – Braiam
    Commented Nov 10, 2014 at 23:12
  • 1
    @Braiam What specific edits would I make to the patch? I'm basically wondering if I can avoid having to specify any target paths in the patch command.
    – Sparhawk
    Commented Nov 10, 2014 at 23:14
  • Some context, please. Are you trying to apply this patch to a running system? If so, why? What is your distribution/OS? What does this patch do? It looks like a patch for a KDE thing. What component? I general, if you distribution isn't applying the patch for you, you should rebuild the relevant package from source with the patch applied. And you can trim of the leading portion of the path. That shouldn't be a problem. How much you trim depends on what the source looks like. Commented Nov 11, 2014 at 2:56
  • 1
    @FaheemMitha Yes, it's running, with Arch/KDE. It's to fix this bug. The fix works if I manually apply changes. However, I don't think that context matters so much in this case, as I can replicate this problem with a test file in /tmp/foo. I'm more interested in a generic answer that applies to patch.
    – Sparhawk
    Commented Nov 11, 2014 at 3:01

2 Answers 2


Looking at the source code of GNU patch, this behavior is built in since version 2.7. As of GNU patch 2.7.1, only relative paths not containing .. are accepted, unless the current directory is the root directory.

To apply a patch containing absolute paths, you can use

(cd / && sudo patch -p0) <foo.patch

In recent versions of GNU patch, you can simply

sudo patch -d/ -p0 <foo.patch
  • Okay, it is what it is. However, if you check my commands, cd /usr also failed, despite the target files being under this directory.
    – Sparhawk
    Commented Nov 11, 2014 at 2:49
  • @Sparhawk Sorry, my description was partly incorrect. The test is: if the current directory is not /, then reject any absolute path or path containing a .. component. Commented Nov 11, 2014 at 3:11
  • 8
    Or more simply, sudo patch -d/ -p0 <foo.patch
    – Qian
    Commented Feb 27, 2017 at 21:22
  • @TheoYou I missed that comment at the time. You should write it up as an answer.
    – Sparhawk
    Commented Dec 12, 2017 at 4:55

With GNU patch 2.7.6 at least, the filename can be specified:

patch /usr/share/apps/plasma/packages/org.kde.pager/contents/ui/main.qml /tmp/fix_kde_icons.patch
  • Thanks for posting @gerardw. This also works well.
    – Sparhawk
    Commented Mar 10, 2020 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .