10
4
I am trying to find a standard POSIX way to duplicate one file's permissions to another file. On a GNU system this is easy:
[alexmchale@bullfrog ~]$ ls -l hardcopy.*
-rw-r--r-- 1 alexmchale users 2972 Jul 8 20:40 hardcopy.1
---------- 1 alexmchale users 2824 May 14 13:45 hardcopy.4
[alexmchale@bullfrog ~]$ chmod --reference=hardcopy.1 hardcopy.4
[alexmchale@bullfrog ~]$ ls -l hardcopy.*
-rw-r--r-- 1 alexmchale users 2972 Jul 8 20:40 hardcopy.1
-rw-r--r-- 1 alexmchale users 2824 May 14 13:45 hardcopy.4
Unfortunately, the --reference flag to chmod is a non-standard option. So that is out for my purposes. I would prefer it to be a one-liner, but that's not necessary. Ultimately, it does need to be in POSIX sh syntax.
The first cp command,
cp has template
, should usecp -p
to preserve the mode and ownership attributes. – mernst – 2014-12-23T05:40:07.720@mernst: That's only necessary for the first
cp
if the owner/group of the file (e.g. "user") is different from the one doing the copying (e.g. root). – Paused until further notice. – 2014-12-23T14:03:54.160@Dennis Willamson: OK, but that is a possibility and I don't see any downside to using
cp -p
there. – mernst – 2014-12-24T15:08:11.327Now that's an interesting approach. I'm going to test this out and see how well it works against various servers. It seems to me like it'll do the trick. – Alex – 2010-08-01T00:00:17.607
@Alex: make sure to test it with file ownership, too, if that's a concern. – Paused until further notice. – 2010-08-01T01:28:28.583