I am trying to copy a large folder structure between machines. I want to maintain the ownership/rights during the copy as it is not reasonable to 'fix' the privs afterwards.

Therefore, I am using the following command to tar the file with privs intact and transfer the data to the destination machine. The same users exist on both machines.

tar cfzp - foldertocopy | ssh me@machine "cat > /applications/incoming/foldertocopy.tar.gz"

The transfer works fine, and the next step is to su to root on the remote machine and untar the file.

The problem is: there isn't enough disk space to store both the compressed and uncompressed data at the same time.

I could use rsync/recursive scp but my user doesn't have the rights to create the files with the correct privs itself and root can't log in remotely.

What are my options? The source machine is RHEL4 and the destination is RHEL5.


As root, set up a named pipe:

# mkfifo /tmp/fifo
# chmod o+w /tmp/fifo

Then, transfer your data as me:

$ tar cfzp - foldertocopy | ssh me@machine "cat > /tmp/fifo"

But read it as root:

# tar -xfzp /tmp/fifo
  • 1
    That worked perfectly, thanks! Unix rocks :) – Rich Jan 30 '12 at 13:59

One solution to the problem is to have ssh run the untar directly:

tar cfzp - foldertocopy | ssh me@machine "cd rightplace; tar xzf -"
  • Will that work even though my user on the remote machine doesn't have the rights to create the files with the correct owner/privs? I can sudo su - on the remote machine, but I don't know if it's possible to do this from within the command executed by ssh...I have to specify a password when doing sudo su -. – Rich Jan 30 '12 at 13:41
  • This is the right solution, but you should do it backwards. SSH to the machine, write a shell script that logs back to your machine and uses tar cf ... | tar xf to transfer the data, and then sudo ./shellscript. You can make the shell script owned by root and only readable by root because your login password will be in it. And delete it after the transfer. – Michael Dillon Feb 1 '12 at 7:51

Why don't you use archival rsync?

rsync -avzHAX foldertocopy user@remoteserver:/file/path/to/copy

This way you can save every detail about the file, also with "z" rsync compresses the stream on the fly.

  • Will that work if user@remoteserver can't create the files with the right privs? – Rich Jan 30 '12 at 15:30
  • No, but you should be able to run it in sudo. – Simon Richter Jan 30 '12 at 17:56
  • rsync will copy files but will fail to set the owners. I'm always working with root so I never experienced it. So yes, this solution may not help you. But there's always root :) – bayindirh Jan 30 '12 at 19:29

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.