5

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?

BenMorel
  • 4,507
  • 10
  • 57
  • 85

2 Answers2

12

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

user9517
  • 115,471
  • 20
  • 215
  • 297
  • 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 ;) – user9517 Dec 17 '13 at 16:57
5

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
MikeyB
  • 39,291
  • 10
  • 105
  • 189