Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Say I have two files:

-rw-rw-r--  1 webapp webapp   215 Jun 21  2012 index.php
-rw-rw-rw-  1 root   root      58 Dec 17 11:02 patch.log

I would like to give patch.log the same permissions as index.php.

I can do it manually:

chown webapp:webapp patch.log
chmod 664 patch.log

But this should be part of a script, where I don't necessarily know what the exact permissions of index.php are.

Is there a way to copy permissions of a given file to another file?

share|improve this question
    
Downvoter, care to explain? –  Benjamin Dec 17 '13 at 11:50

2 Answers 2

up vote 10 down vote accepted

You can use a file as a reference file for both chown and chmod

chown --reference=index.php patch.log
chmod --reference=index.php patch.log

It's all in the man pages btw

chown

--reference=RFILE use RFILE’s owner and group rather than specifying OWNER:GROUP values

chmod

--reference=RFILE use RFILE’s mode instead of MODE values

share|improve this answer
    
Interesting. Never seen that before. But if you can read octal permissions... –  ewwhite Dec 17 '13 at 11:56
1  
It's be really nice if GNU manpages specified when they added arguments/functionality outside of the POSIX standard. This works in GNU chown and chmod, but not in any other OSes. –  Chris S Dec 17 '13 at 16:55
    
@ChrisS: The question is tagged linux ;) –  Iain Dec 17 '13 at 16:57

If there's any possibility of extended ACLs on the files in question, it's better to use getfacl/setfacl:

getfacl index.php | setfacl --set-file=- patch.log
share|improve this answer

Your Answer

 
discard

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.