How to copy permissions from one file to another on Linux

See all GNU/Linux related FAQ
I need to copy or clone file ownership and permissions from another file on Linux. Is there a bash command line option to clone the user, group ownership and permissions on a file from another file on Linux operating system?

To set file owner and group use chown command. To change file mode, bits (permissions) use chmod command. Both commands accept an option to use another file as a reference is known as RFILE. In other words, you can copy permissions from one file to another in Linux using the chmod command along with the --reference option. Here’s how to do it.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements none|Linux terminal
Category File Management
OS compatibility AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora Linux Mint Mint openSUSE Pop!_OS RHEL Rocky Slackware Stream SUSE Ubuntu
Est. reading time 2 minutes

Syntax to clone permissions from another file or directory on Linux using the chmod command

The syntax is as follows use RFILE’s mode instead of MODE values:
chmod --reference=RRFILE FILE
chmod [options] --reference=RRFILE FILE
chmod --reference= target_file

Examples: Copy file permission, but not files

Let us list both files:
$ ls -l install58.iso xenial-server-amd64.iso
Sample outputs:

-rw-rw-rw- 1 libvirt-qemu kvm 230862848 Aug 16  2015 install58.iso
-rw-r--r-- 1 libvirt-qemu kvm 786432000 Mar 14 02:01 xenial-server-amd64.iso

To copy install58.iso file permission to xenial-server-amd64.iso, enter:
$ chmod --reference=install58.iso xenial-server-amd64.iso
Verify it:
$ ls -l install58.iso xenial-server-amd64.iso
Sample outputs:

-rw-rw-rw- 1 libvirt-qemu kvm 230862848 Aug 16  2015 install58.iso
-rw-rw-rw- 1 libvirt-qemu kvm 786432000 Mar 14 02:01 xenial-server-amd64.iso

You can specify multiple files too:
$ chmod --reference=file.txt dest1.txt dest2.txt dest3.conf
You can combine and use find commandand xargs command as follows:
$ find /path/to/dest/ -type f -print0 | xargs -O -I {} chmod --reference=/path/to/rfile.txt {}

Syntax to clone ownership from another file or directory on Linux using the chmod command

The syntax is as follows to use RRFILE’s owner and group rather than specifying OWNER:GROUP values
chown --reference=RRFILE FILE
chown [options] --reference=RRFILE FILE

Examples: Copy file ownership, but not files

To copy install58.iso file user and group onwership to xenial-server-amd64.iso, enter:
chown --reference=install58.iso xenial-server-amd64.iso
ls -l

Sample outputs:

Fig.01: Linux clone or copy or replicate file permissions, using another file as reference

Fig.01: Linux clone or copy or replicate file permissions, using another file as reference

Copying with Access Control Lists (ACLs) under Linux

Linux supports ACLs too. Use the getfacl filename to see if if ACLs are enabled:
$ getfacl foo.txt
If you see output beyond typical Linux permissions, ACLs are enabled.

# file: foo.txt
# owner: vivek
# group: vivek
user::rw-
user:chinu:rw-
user:marlena:rw-
group::rw-
mask::rw-
other::r--

Next, Copy ACLs on Linux as follows:
$ getfacl reference_file | setfacl -b -n -M - target_file
$ getfacl reference_file | setfacl --set-file=- target_file
getfacl foo.txt | setfacl --set-file=- bar.txt
$ getfacl foo.txt | setfacl -b -n -M - bar.txt

Where:

  --set-file=file  Read ACL entries to set from file
  -M Read ACL entries to modify from file
  -b Remove all extended ACL entries
  -n Don't recalculate the effective rights mask

Wrapping up

You learned how to copy permissions from one file to another on Linux, along with explanations and considerations using the chown command, chmod command and getfacl command/setfacl command. For more info see the following manual pages using the help command or man command:
$ man chown
$ man chmod
$ man getfacl
$ man setfacl

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

6 comments… add one
  • Chris S. Mar 17, 2016 @ 20:16

    I had never thought about needing this before and now that I see how it’s done can’t believe I haven’t used chmod/chown in this way before. Thanks!

  • Naveen D Mar 18, 2016 @ 9:29

    You can acieve this same result in single command using ACL

    getfacl file1 | setfacl --set-file=- file2
  • Emily Short Mar 18, 2016 @ 11:19
    drw-r--r--.  2 emily emily   4096 Mar 18 14:58 z_dir
    	-rw-rw-r--.  1 root   emily      0 Mar 18 14:56 z_file1
    	-rw-rw-r--.  1 emily root        0 Mar 18 14:56 z_file2
    	[root@oc1700126186:/home/emily]# ll z_dir
    	total 0
    	-rw-r--r--. 1 root root 0 Mar 18 14:58 zfile3
    	-rw-r--r--. 1 root root 0 Mar 18 14:58 zfile4
    

    copy permission from file to directory recursively
    —————————————————

    	[root@oc1700126186:/home/emily]# chmod -R --reference=z_file2 z_dir
    	[root@oc1700126186:/home/emily]# ll z_dir
    	total 0
    	-rw-rw-r--. 1 root root 0 Mar 18 14:58 zfile3
    	-rw-rw-r--. 1 root root 0 Mar 18 14:58 zfile4
    

    copy ownership from file to directory recursively
    ————————————————–

    	[root@oc1700126186:/home/emily]# chown -R --reference=z_file2 z_dir
    	[root@oc1700126186:/home/emily]# ll z_dir
    	total 0
    	-rw-rw-r--. 1 emily root 0 Mar 18 14:58 zfile3
    	-rw-rw-r--. 1 emily root 0 Mar 18 14:58 zfile4
    	[root@oc1700126186:/home/emily]# 
    

    copy for multiple directory/files
    ———————————-

    	[root@oc1700126106:/home/cleri]# chown -R --reference=z_file2 z_dir1 z_dir2 z_dir3
    	[root@oc1700126106:/home/cleri]# chmod -R --reference=z_file2 z_dir1 z_dir2 z_dir4
    
  • Launchpad McQuack Mar 19, 2016 @ 2:48

    I always use:

    find . | cpio -pVmud /destination/directory
  • Eddie G. Mar 21, 2016 @ 1:46

    I wonder, can this be used to clone a “home” folder from a failing drive and placed in a new install of the same OS?….will there be UUID conflicts?…what about other unique information?

    • margaret Jan 13, 2017 @ 19:19

      for cloning a home directory, including permissions, use rsync:

      % rsync -avP /source/ /destination/

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.