Blog

03/06/2019

How to copy permissions from one file to another on Linux



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.

Syntax to clone permissions from another file or directory on Linux

The syntax is as follows use RFILE’s mode instead of MODE values:
chmod --reference=RRFILE FILE
chmod [options] --reference=RRFILE 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 and xargs 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

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

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....