4

I have a list of files: /location/file.txt

Content of file:

/location1/file2.abc
/location2/location3/file1.abc
/location4/location5/fileX.cde

I'd like to copy files to another sever like this:

cat /location/file.txt | xargs -i scp {} user@server:/location

but in the above example all of files from the list are copying to specific location, while I'd like to copy them to the same locations (accurate path as in the txt file /location1/, /location2/location3/ etc.

How can I do it? I can also use rsync, but I'd like to enter password (to remote server) only once.

  • FYI: to avoid multiple-password-entry, if you are using SSH keys, you can use ssh-agent and ssh-add to enter the key's password only once. – Otheus May 12 '15 at 16:28
5
rsync -av --files-from=/location/file.txt / user@server:/location/

That will copy the local file /location1/file2.abc to /location//location1/file2.abc etc.

As rsync will invoke ssh only once, you only have to enter the password once. You may want to investigate using ssh keys though.

  • Ok, but if I want to copy all of files from the list to exactly same location on remote server I should use following command? rsync -av --files-from=/location/file.txt / user@server:/ – Qmski May 12 '15 at 13:50
  • 1
    @Qmski almost. You will need to add the -R flag, too: rsync -avR ... – roaima May 12 '15 at 13:58
  • In case your list of files includes a directory tree which you want to copy, and in addition to the -av and -R ("relative"), you will also need the seemingly-redundant-but-not r flag. From the man page regarding -a : "The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied." – Otheus May 12 '15 at 16:29
  • @roaima note that --files-from implies -R so you don't have to add that! – wurtel May 13 '15 at 11:41
  • @Otheus you usually will want to preserve permissions, owner, group, time etc. so I usually just use -a; but at least -ptgo should be used. – wurtel May 13 '15 at 11:45

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.