linux copy file permissions and ownership

How to Copy File Permissions & Ownership from Another File in Linux

Let us say you have copied a file in Linux, or created a new file. In most cases, this file will have the default permission set for all files in the system. But sometimes you may want this new file to have the exact same permissions and ownership as another file on your system. In such cases, you can easily copy the file ownership and permissions from the old file to new file, using chown and chmod commands respectively. In this article, we will learn how to copy file permissions & ownership from another file in Linux.


How to Copy File Permissions & Ownership from Another File in Linux

Here are the steps to copy file permissions & ownership from another file.


Copy File Permissions to Another File

Let us say you have two files data1.txt and data2.txt and you want to copy file permissions from data1.txt to data2.txt file. In this case you need to use –reference option followed by the path to file whose permission you want to copy. In this case, it is data1.txt. This should be followed by the file path of file whose permission you want to modify.

$ chmod --reference=data1.txt data2.txt
$ ls -l data2.txt

You can also use this method to copy file permission to multiple files. Here is the command to copy file permission from data1.txt to data2.txt, data3.txt, data4.txt.



Copy File Ownership to Another File

Similarly, to copy ownership of one file to another file, you need to use –reference option, followed by the file path of file whose ownership you want to copy. This should be followed by the file whose ownership you want to change. Here is the command to copy ownership of file data1.txt to data2.txt.

$ chown --reference=data1.txt data2.txt
$ ls -l data2.txt

Please note, in this case, we will not be using owner:group format to specify file ownership.

You can also use this method to copy the ownership of one file to multiple files. Here is the command to copy file ownership from data1.txt to data2.txt, data3.txt, data4.txt.

$ chown --reference=data1.txt data2.txt data3.txt data4.txt

In this article, we have learnt how to copy file ownership and permissions from one file to another using chown and chmod commands respectively. You can use this to copy permissions and ownership across large number of files in a directory. For example, let us say you have copied hundreds of file from /home/ubuntu to /home/data, then here is a command to copy the ownership and permissions of all files present in /home/ubuntu to /home/data.

#!/bin/bash
for filename in /home/ubuntu/*; do

        chmod --reference=/home/ubuntu/"$filename" /home/data/"$filename"


        chown --reference=/home/ubuntu/"$filename" /home/data/"$filename"
done

In the above code, we basically loop through all the files in /home/ubuntu and run chmod and chown commands on each of them one by one, such that each file’s ownership and permissions are copied to the corresponding file in /home/data. We have assumed that the filenames in both folders are the same, after you have copied the files.

Also read:

Display Command Output & File Content in Column Format
How to Create Nested Directory in Python
How to Add Blank Directory in Git Repository
How to Iterate Through Files in Directory in Python
How to Find All Text Files in Directory in Python

Leave a Reply

Your email address will not be published. Required fields are marked *