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?
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).
Save original permissions to
$ getfacl filename.txt > acl.txt
Overwrite 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.txt
setfaclto restore correct permissions from
$ 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
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 you
cdto a consistent location on both machines)
# cd / # getfacl -R usr > /root/acls.txt
Copy 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