1

I would like to know best way to copy directory structure from one server(Unix) to another (Linux):

  • without root user
  • servers are not connected (e.g. can't use SSH directly between them)
  • I need to copy only folders and subfolders, without files
  • permissions on folders must also bi transferred

So i have user@server1 with some directory structure. I need to copy this structure to user@server2 without files, with same permissions. Users on both servers have same permissions and are in the same group, servers are not connected.

Edit: I forgot to mention that server1 is Unix, and server 2 is Linux. So when I try to execute proycon's code on Unix server I got message: tar: Not a recognized flag: z When I try Tom Hunt's option I got message: -printf is not a valid option. Also, worth to mention, user have different names, although they have same rights and are in same group. So I have user_xy@server1 and user_zx@server2

  • Not connected so you're looking for some offline approach (e.g. create tar file) ? – steve Sep 25 '15 at 20:50
3

Ideally you'd do something like rsync or scp, but then the machines would indeed have to be connected. I'd go for using tar with find instead if you have no means of direct transfer, it can preserve users, permissions, and symlinks.

On one host:

$ find yourdirectory/ -type d | tar -cvzf archive.tar.gz --no-recursion --files-from -

Transfer the archive by any means (ftp? USB stick? smoke signals?)

On target host:

$ tar -xvzf archive.tar.gz
2

On server1:

find . -type d -printf '%p\n%m\n' >dir_list

Transfer the file dir_list from server1 to server2 however you see fit.

On server2:

while read -r filename; do
    read -r perms
    mkdir -p "$filename"
    chmod "$perms" "$filename"
done <dir_list

This will create all the directories owned by the user you run as on server2. Changing the ownership cannot be done without superuser.

  • This breaks if there is a directory with a carriage return in its name, though that's probably rare enough it can be handled manually. – drewbenn Sep 25 '15 at 21:42
  • 1
    True. If handling this is necessary, you can use -printf '%p\0%m\0' and read -d ''. – Tom Hunt Sep 25 '15 at 21:44
  • Hi, thanks for answers. Yes, both answers would be what I need. I forgot to mention that server1 is Unix, and server 2 is Linux. So when I try to execute proycon's code on Unix server I got message: tar: Not a recognized flag: z When I try Tom Hunt's option I got message: -printf is not a valid option. Also, worth to mention, user have different names, although they have same rights and are in same group. So I have user_xy@server1 and user_zx@server2 – botafogo Sep 27 '15 at 14:22

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.