I have two files in a directory. One has correct permissions and the other has not. Is there a way I can "copy" the set of permissions from one file to another?
4 Answers
The GNU version of the chmod utility can copy the mode from one file (RFile) to another (file).
chmod --reference=RFile file
GNU coreutils is found in most Linux distributions and Cygwin, among other places. Not all chmod implementations provide this option.
- 42,640
- 2,878
try this:
find /PATH/TO/TARGET -exec chmod --reference /PATH/TO/SOURCE/{} {} \;
this would go up recursivly and chmod every file, if two directory don't match on files you will see lots of "No such file or directory" error.
- 111
- 4
-
find /home/myubuntuuser/Desktop/test1 -exec chmod --reference /home/myubuntuuser/Desktop/test2/{} {} \;– Rick Sanchez Jun 03 '18 at 12:18 -
chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/111.txt': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/222.txt': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/333.txt': No such file or directory – Rick Sanchez Jun 03 '18 at 12:18
-
tested it on 2 folders: test1 and test2. each have the same files 111/222/333.txt with different permissions. test1 has the default ones. test2 has 777 permissions. this is the error i get. – Rick Sanchez Jun 03 '18 at 12:19
I came up with this:
find $SOURCE -mindepth 1 -printf 'chmod --reference=%p\t%p\n'|sed "s/\t$SOURCE/ $DEST/g"|sh
It is not fully bullet proof, but does what I need.
- 131
You can use getfacl to retrieve the full listing of file permissions, owner, group, and additional ACLs (access control lists).
$ getfacl filename.txt
# file: filename.txt
# owner: score
# group: score
user::rw-
group::---
other::---
If you save that output to a file (e.g. acl.txt), you can then restore from this format with setfacl --restore acl.txt. If you only want to restore a single file, and that file has a different filename to the original, you will want to use setfacl --set-file acl.txt filename.txt (where filename.txt is the new filename).
Steps
Save original permissions to
acl.txt:$ getfacl filename.txt > acl.txtOverwrite permissions (for demonstration; this is just so that you can see that restoring it in the next step works)
$ chmod 777 filename.txt $ sudo chown nobody:root filename.txt $ ls -l filename.txt -rwxrwxrwx 1 nobody root 0 Jan 8 14:24 filename.txtUse
setfaclto restore correct permissions fromacl.txt:$ sudo setfacl --restore acl.txt $ ls -l filename.txt -rw------- 1 score score 0 Jan 8 14:24 filename.txt
The filename is taken from the # file: comment generated by getfacl, so there is no need to specify it on the command line.
If you want to restore those permissions to a different file, you can use --set-file instead of --restore like so:
$ setfacl --set-file acl.txt second_filename.txt
Example
If you end up overwriting the permissions on some files in /usr, but you don't know what files you have overwritten, you can usually fix it by restoring from another similarly configured system.
Backup permissions from working system (note:
getfaclgenerates relative paths, so ensure youcdto a consistent location on both machines)# cd / # getfacl -R usr > /root/acls.txtCopy the ACL dump to the system with broken permissions
$ scp root@working-system:/root/acls.txt . $ scp acls.txt root@broken-system:/root/Restore the ACL dump to overwrite the broken permissions with those from the known good machine
# cd / # setfacl --restore /root/acls.txt
- 951
--referenceoption is included in the GNU version; OSX probably uses a chmod that originates with BSD instead. OSX man chmod : http://developer.apple.com/Mac/library/documentation/Darwin/Reference/ManPages/man1/chmod.1.html – quack quixote Feb 28 '10 at 16:31cp -dpR <source-file> <dest-file>will, when copying a file, copy permissions as well as the file.r – LawrenceC Mar 14 '11 at 13:57