Advance

How to copy file permissions and Ownership to another file in Linux

How to copy file permissions and Ownership to another file in Linux

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.

Recommended Article: How to configure Cowpatty on ubuntu 20.04

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    view permission on linux      

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:

How to disable SELinux temporarily or 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

view key list on linux

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

 

If you still need more information, have a look at:

21 Most Used Commands in SSH and Linux

How to run a command with the time limit in Linux

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.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day