Qadry
unixcop Admin

How to copy or clone permissions from one file to another on Linux

Introduction

Suppose that you have just created a new file and want it to have the same permissions and ownership of an older file.

Need to copy or clone the older file ownership and permissions to the new file you created?

In this small guide we will learn how to clone the user, group ownership and permissions on a file from another file on Linux operating system

  • To change file owner and group use chown command.
  • To change file permissions use chmod command.

Note:

RFILE in below on the articles refers to file as a reference.

Copy File Permissions to Another File

To copy file permissions of old file to another file, use chmod command with the –reference.

Where reference_file is the file from permissions will be copied.

$ chmod --reference=RRFILE file
$ chmod [options] --reference=RRFILE file

For example:

Copy file permission, but not files

[root@unixcop ~]# ls -l unixcop 
--w-r-----. 1 root root 0 Sep 21 17:44 unixcop
[root@unixcop ~]# 
[root@unixcop ~]# ls -l monitor.txt 
-rw-r--r--. 1 root qadry 0 Sep 21 17:46 monitor.txt
[root@unixcop ~]# chmod --reference=unixcop monitor.txt 
[root@unixcop ~]# 
[root@unixcop ~]# ls -l monitor.txt 
--w-r--r--. 1 root qadry 0 Sep 21 17:46 monitor.txt
[root@unixcop ~]#

Note:

You can list the permissions of the both files at the same time with:

[root@unixcop ~]# ls -l unixcop monitor.txt
--w-r-----. 1 root root 0 Sep 21 17:44 unixcop
--w-r--r--. 1 root qadry 0 Sep 21 17:46 monitor.txt
[root@unixcop ~]# 

Copy File Ownership to Another File

Also to copy the ownership from another file, use chown command with the –reference as shown in the command below

Where reference_file is file from which owner and group will be copied.

$ chown --reference=reference_file file

For example,

[root@unixcop ~]# ls -l monitor.txt
--w-r--r--. 1 root qadry 0 Sep 21 17:46 monitor.txt
[root@unixcop ~]#
[root@unixcop ~]# touch servers
[root@unixcop ~]#
[root@unixcop ~]# ls -l servers
-rw-r--r--. 1 root root 0 Sep 21 17:50 servers
[root@unixcop ~]#
[root@unixcop ~]# chown --reference=monitor.txt servers
[root@unixcop ~]#
[root@unixcop ~]# ls -l servers
-rw-r--r--. 1 root qadry 0 Sep 21 17:50 servers
[root@unixcop ~]#

Copy files permissions to multiple files

You can also copy file permissions and ownership from an old file to multiple files as shown below.

$ chmod --reference=unixcop.txt unixcop1.txt unixcop2.txt unixcop3.txt
$ chown --reference=unixcop.txt unixcop1.txt unixcop2.txt unixcop3.txt

Also we can combine, find and xargs as follow with shown command:

$ find /path/to/dest/ -type f -print0 | xargs -O -I {} chmod --reference=/path/to/rfile.txt {}

Conclusion

In this article, we illustrated how to copy or clone the permissions and ownership from one file to another file in Linux using chmod and chown commands.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

x