How to clone permissions from another file or directory on Linux

chmod command is used to set permission bit on file or directory. It also allows to clone/copy permissions of one file to another. For example we have two files with following permissions:

$ ls -l file*
-rwxr-xr--. 1 lrendek lrendek 0 Apr  7 14:39 file1
-rw-rw-r--. 1 lrendek lrendek 0 Apr  7 14:40 file2

Both files have different permissions. Using --reference option the chmod command is capable to clone permissions from one file to another. For example the below command will clone permissions of file1 and use it as a reference to set permissions on file2:

$ chmod --reference=file1 file2

Permissions of the file2 file were cloned and set to be the same as permissions of file1 file.

$ ls -l file*
-rwxr-xr--. 1 lrendek lrendek 0 Apr  7 14:39 file1
-rwxr-xr--. 1 lrendek lrendek 0 Apr  7 14:40 file2

The same syntax also applies for directories:

$ ls -ld dir*
d--x--x--x. 2 lrendek lrendek 40 Apr  7 14:52 dir1
drwxrwxr-x. 2 lrendek lrendek 40 Apr  7 14:52 dir2
$ chmod --reference=dir1 dir2
$ ls -ld dir*
d--x--x--x. 2 lrendek lrendek 40 Apr  7 14:52 dir1
d--x--x--x. 2 lrendek lrendek 40 Apr  7 14:52 dir2