A Linux system administrator needs to know some Linux tricks. In this article, you are going to learn how to copy file permissions and Ownership to another file in Linux. It is so normal to face a situation that you need to have the same permissions and ownership of an older file when you have two files or you have just created a new file.
Using chmod and chown commands respectively, you would learn how to do this.
How to copy file permissions and Ownership to another file in Linux
In case you have two files or you have just created a new file and want it to have the same permissions and ownership of an older file.
Copy File Permissions to Another File
When you need to copy file permissions from one file to another file, you can use chmod command with the –reference switch in the following Linux syntax, where reference_file is the file from which permissions will be copied rather than specifying mode for a file.
chmod --reference=reference_file file
You can see the below as an example.
ls -l users.list $ ls -l keys.list $ sudo chmod --reference=users.list keys.list $ ls -l keys.list
Buy Linux Virtual Private Server
Copy File Ownership to Another File
And again when you decide to copy ownership from another file, you can use chown command with the –reference switch as well using the following syntax, where reference_file is a file from which owner and group will be copied rather than specifying owner:group values for the file.
chown --reference=reference_file file
You may also be interested in:
https://eldernode.com/disable-selinux-temporarily-permanently/
Now, let’s see the above command example.
ls -l keys.list touch api.list ls -l keys.list sudo chown --reference=keys.list api.list ls -l api.list
Also, you can use the below command to copy permissions and ownership from one file to multiple files
sudo chmod --reference=users.list users1.list users2.list users3.list sudo chown --reference=users.list users1.list users2.list users3.list
In case you prefer to get more information, refer to the chown and chmod man pages.
man chown man chmod
Dear user, we wish this tutorial would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.