I need to sync only ownership and group of each file and dir between two server filesystems. That's to say if I have server A and server B, I don't need and don't want to copy data from one server to other, I need to set the same owner and group from each file in B that exists in A. If a file/dir doesn't exist in A, or viceversa, I don't need to do nothing about, only to change owner and group of every file and directory that exists in both servers to the one of the file/dir in server A. I know that "chown --reference" make that, but I don't know how to use it for change according to other files and directories.

I wish I explained my question correctly.

Please, any suggestion?

Thanks in advance

  • Just owner and group, and neither permission nor SELinux security context nor ACLs? (Just checking.) – Ulrich Schwarz Feb 8 '18 at 8:24
  • Yes, only OWNER and GROUP, not data content or permissions – Rafael Linux User Feb 8 '18 at 8:28

Assuming that you don't mind trying to set ownerships on files that already have them correctly, I guess you could run something along the lines of

/bin/stat --format 'chown %u:%g %n' ~/.bash*

on server A, which will give you a list of chown commands to execute on server B. (You could pipe the whole thing through | ssh user@serverB.)

You'll get the occasional error for files that don't exist on B, but that would be harmless.

  • I don't mind to "re-set" files that have yet the correct owner and group, as you correctly thought. I think your idea is fantastic, I didn't imagine to use "stat". I was wondering on using "rsync" combined with "chown --reference", but your solution is less risky. I'll give you news about the task. Thank you!! – Rafael Linux User Feb 8 '18 at 8:40
  • @RafaelLinuxUser, try stat --format 'chown %U:%G "%n"' to have names – JJoao Feb 8 '18 at 8:54
  • Thank you!! I remove my question, cause I should have investigate more on the paremeters of "stat" before asking. – Rafael Linux User Feb 8 '18 at 9:27
  • Based on Ulrich solution, I have this sentence: "find / ! -user root -or ! -group root | xargs stat --format 'chown %U:%G %n' ", but , please, how to redirect to a txt file? I tried adding "> text.txt" but didn't worked. – Rafael Linux User Feb 8 '18 at 9:29
  • 1
    @JJoao: yes, always a tricky issue if you don't know if uid/guid assignments are the same for both servers. – Ulrich Schwarz Feb 8 '18 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.