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?


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.

  • Hm... looks like this is supposed to work, but not supported on Mac OS X? There I only get illegal option... – Svish Feb 28 '10 at 16:18
  • 2
    chmod is not a bash builtin command. it is a separate utility available on many unixes. the --reference option is included in the GNU version; OSX probably uses a chmod that originates with BSD instead. OSX man chmod : developer.apple.com/Mac/library/documentation/Darwin/Reference/… – quack quixote Feb 28 '10 at 16:31
  • Svish, you might consider installing the GNU versions through MacPorts. – Jeremy L Mar 1 '10 at 23:31
  • Just figured that it would be useful to mention here that cp -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

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.


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.

  • find /home/myubuntuuser/Desktop/test1 -exec chmod --reference /home/myubuntuuser/Desktop/test2/{} {} \; – Rick Sanchez Jun 3 '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 3 '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 3 '18 at 12:19

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

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).


  1. Save original permissions to acl.txt:

    $ getfacl filename.txt > acl.txt
  2. 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
  3. Use setfacl to restore correct permissions from acl.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


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.

  1. Backup permissions from working system (note: getfacl generates relative paths, so ensure you cd to a consistent location on both machines)

    # cd /
    # getfacl -R usr > /root/acls.txt
  2. Copy the ACL dump to the system with broken permissions

    $ scp root@working-system:/root/acls.txt .
    $ scp acls.txt root@broken-system:/root/
  3. Restore the ACL dump to overwrite the broken permissions with those from the known good machine

    # cd /
    # setfacl --restore /root/acls.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.