4

Is it possible to get rsync to read a list of file to include from an array in the active script rather than a accompanying file?

## declare an array variable
declare -a files=("path" "path" "path")

rsync --files-from=${files[@]} source destination

Is there a better way to do this? I'd like to have just one script file to manage.

6

You can execute :

`rsync --files-from=<( printf "%s\n" "${files[@]}" ) source destination
  • <() wil open a pipe and fill it with the content of the inner command. It returns a file descriptor

  • printf "%s\n" "${files[@]}" echo each file name in a different line

3
  • Thanks @vera. Unfortunately I'm getting this:+ myFiles=(1\n 2\n 3\n) + rsync -avh --files-from=/dev/fd/63 /foo/ /bar/ ++ echo -e 1n 2n 3n building file list ... rsync: link_stat "/foo/1n 2n 3n" failed: No such file or directory (2) 0 files to consider
    – sansSpoon
    Sep 23 '17 at 14:45
  • Ok, I see the problem: you can use printf instead of echo, then you do not have to add the trailing "\n". I edited the answer.
    – vera
    Sep 25 '17 at 6:53
  • For me this doesn't work i got error with break-lines interpreted as #012. I had to use null character : rsync --files-from=<( printf "%s\0" "${files[@]}" ) --from0 source destination Oct 12 '18 at 14:19

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.