How to clone permission of a directory of file onto another file or directory in Linux
In my day job I recently came across a much easier was to deal with file and/or directory permissions on Linux based systems. Typically, to determine the permissions of a file or directory you would use the ls command with various potions appended to the end of the command and to change the same file or directory’s permissions you would use the chmod command. As I recently discovered the chmod is also able to clone or copy permissions as well as manually set them.
To lustrate what I ma talking about let’s create a hypothetically example where we have two files with the 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
If we wanted to copy the permissions from file 1 onto file2 we would run the following option appended to the chmod command.
$ chmod --reference=file1 file2
As we can see the permissions for file2 have been cloned from file1.
$ 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
If we extend this example further we can also clone the permissions of a directory as well!
$ 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
I had a little bit of an issue finding this but stubbled across this site that provided me with the syntax and examples. Although, I am making this entry mainly for myself but I also hoping that this post will make another Sys. Admin. Job easier. Uf you have any questions, comments, etc. please feel free to contact me on my Contact page or on Twitter.