3

I have a list of files lying in subdirectories which I have created by calling find:

for f in $(find . -name "myFile.txt"); do 
    echo "$f" >> filelist.txt;
done

The content of filelist.txt looks like this:

./First/Path/To/File/myFile.txt
./Second/Path/To/File/myFile.txt
./Third/Path/To/File/myFile.txt
...

Now I want to copy all these files to a remote computer such that they are placed in the corresponding folders:

remoteComputerName:/some/root/directory/First/Path/To/File/myFile.txt
remoteComputerName:/some/root/directory/Second/Path/To/File/myFile.txt
remoteComputerName:/some/root/directory/Third/Path/To/File/myFile.txt

However on the remote computer the folder structure First/Path/To/File/ etc, does not exist yet and also I don't want to copy the entire directories but only the file myFile.txt in it.

I know that on a local computer this works using the command

while read p; do cp --parents $p /some/root/directory ; done < filelist.txt

However for remote computers using scp this option --parents doesn't work anymore. Also rsync doesn't create a parent directory if it's missing. Does someone know a solution?

2
  • I've deleted my answer, because it wasn't good at all. Namely it would have required authenticating twice for each file, which I'd say it's indeed a no deal. However I've pasted it here for the moment, in case you or someone else want to do something with it: paste.ubuntu.com/15538652. If I find a good way to change it so that authenticating twice is no more necessary I'll undelete my answer (in any case heemayl's answer is most likely the way to go about this).
    – kos
    Mar 28, 2016 at 13:19
  • Ah, and the notice about the pitfalls when using for f in $(find [...]): mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29.
    – kos
    Mar 28, 2016 at 13:21

1 Answer 1

5

Using rsync:

rsync -av --files-from=filelist.txt . remote:/some/location/

Run this from the directory where you have run find, the root of the directories, where the filelist.txt currently is.

--files-from reads files (newline separated) to be copied from the file filelist.txt.

--files-from should always use relative path (unlike mentioning directly), although you can be explicit about it:

rsync -av --relative --files-from=filelist.txt . remote:/some/location/
2
  • This seems to work nicely thank you :). I guess the dot . specifies the source directory which is in this case the current directory ?
    – mcExchange
    Mar 28, 2016 at 13:03
  • @mcExchange Yes and the filelist.txt contains files relative to .
    – heemayl
    Mar 28, 2016 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .