0

I have a list of files (absolute path of files/folders) stored in a text file. I need to copy only the permissions, user:group attributes (of all those files) from one machine and apply the same settings on the same set of files on another machine.

One way I can think of is to do it manually one by one, by checking the attributes on one machine and doing chmod/chown on another machine file by file but that seems to be a tedious task.

Any idea of how to automate this ?

edit: Just wanted to make clear that I don't need the data of these files from the source machine because the data is different in source machine. Target machine contains the updated data, only thing that I need now from source machine is file/folder permission and user:group.

RunSingh
  • 69
  • 8
  • possible duplicate of [Copy file permissions, but not files](http://stackoverflow.com/questions/15245144/copy-file-permissions-but-not-files) – Paul R Aug 23 '13 at 09:33
  • That questions was to copy permissions on the same machine using chmod --reference. This question is how to copy permission across machines. – RunSingh Aug 23 '13 at 10:30

1 Answers1

2

How about this?

#!/bin/bash

user="user"
host="remote_host"

while read file
do
    permission=$(stat -c %a $file) # retrieve permission
    owner=$(stat -c %U $file) # retrieve owner
    group=$(stat -c %G $file) # retrieve group

    # just for debugging
    echo "$file@local: p = $permission, o = $owner, g = $group"

    # copy the permission
    ssh $user@$host "chmod $permission $file" < /dev/null 

    # copy both owner and group
    ssh $user@$host "chown $owner:$group $file" < /dev/null

done < list.txt

I am assuming that the list of the files is saved in "list.txt".

Moreover you should set the variables "user" and "host" accordingly to your setup.

I would suggest to configure ssh to have "automatic login". Otherwise you should insert the password twice per loop. Here a good tutorial to do this SSH login without password.

Another solution that establishes just one ssh connection and uses the recursive option for the directories (as asked in the comments) is the following:

#!/bin/bash

user="user"
host="remote_host"

cat list.txt | xargs stat -c "%n %a %U:%G" | ssh $user@$host '
while read file chmod_par chown_par 
do
    # $file contains %n
    # $chmod_par contains %a
    # $chown_par contains %U:%G

    if [ -d $file ]; then
            chmod -R $chmod_par $file
            chown -R $chown_par $file
    else
            chmod $chmod_par $file
            chown $chown_par $file  
    fi
done'
wooghie
  • 437
  • 2
  • 8
  • +1. Thank you very much for the answer. Really helpful. I need to modify it a little bit to differentiate between files and directories. for directories I have to add -R. – RunSingh Aug 25 '13 at 09:36
  • Another point I thought of is, instead of doing ssh for every file. lets write chmod and chown in a temporary script file and transfer and execute that script on the target machine. Doing this we need to provide password may be at most 2 times, once for scp and another for ssh. Hope it'll improve it a little bit. – RunSingh Aug 25 '13 at 09:40
  • You are perfectly right. The first solution, although very easy to understand, is not efficient. The second one should be faster. If your set of files/directories is big enough and you have time, I would be curios to know the improvement of the second version wrt to first one. Thanks! – wooghie Aug 25 '13 at 12:47