Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a file owned by a non-root user:

# ls -l example.php
-rw------- 1 foo bar ... example.php

I just applied a patch with a command like

patch -p0 <<-EOF
--- .../example.php.orig 2012-06-20 15:07:13.000000000 +0200
+++ .../example.php  2012-06-20 15:43:05.000000000 +0200

as root and the target file user and group were both changed to root. I couldn't find any reference to this in the man pages.

Is there a way to leave the file ownership as before without using su, sudo, or chown?

  • su seems unable to specify a group.
  • sudo -u foo patch ... runs fine, but sets the wrong group.
  • sudo -u foo -g bar patch ... returns Sorry, user root is not allowed to execute '/usr/bin/patch' as foo:bar on hostname. This might be because user foo is not a member of group bar.
  • chown would mean having to store the permissions before running patch, adding another two lines of code.
share|improve this question
Can you show an example with file ownership and the exact command you run? I'm a bit surprised to see that user root is not allowed to execute ... as foo. That would indicate you are logged in as root and then executing sudo. Usually you run sudo to gain elevated privileges. So to help you we need to know who owns the file, who is executing the patch command etc. – Bram Jun 20 '12 at 13:15
@Bram: Clarified the question. – l0b0 Jun 20 '12 at 15:17
up vote 8 down vote accepted

patch creates new file, that's why it holds effective user credentials.

A workaround: use patch -o to have temporary file created, then simply cat tmp file to original file.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.