Using scp to transfer a .txt file list of files

Solution 1:

if you have the text file already created you do the following

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

this will go line by line of the output of the file.txt and run the scp comamand per line; I hope this helps.

Solution 2:

Consider rsync as an alternative, which has ssh support built-in and supports reading from a list of files using --files-from.

Example:

rsync -av --progress --partial-dir=/tmp --files-from=file_list.txt . [email protected]:/dest

Advantages over scp:

  • Avoid cat/xargs and command line length limit, so only enter password once
  • Resume progress after being interrupted
  • Delta transfer algorithm avoids copying files that already exist
  • Generally more robust and featureful
  • Further reading: How does scp differ from rsync?

See also:

  • Do you need -e ssh for rsync?

Solution 3:

I wanted to do the same for list of folders and none of the answers using xargs worked for me. In the end I solved this simply with:

scp -r user@server:/location/{"$(cat folder_list.txt | tr '\n' ',' | sed 's/,$//')"} .

Where I pass my list of files to shell expansion. I think this may be helpful to anyone looking for alternative solution.