I want to do stuff like this:

echo myserver:~/dir/2/ | rsync -r HERE /local/path/

I want the output redirected to a specified location in the command. The echo stuff goes "HERE". What's the easiest way to do this?

  • Is there any purpose with your command that this rsync -r myserver:~/dir/2/ /local/path/ doesn't prove it? – αғsнιη Oct 21 '17 at 9:25
  • Yes, proof of concept. My real script is long and hard to read, so I made this one. – Lumify Oct 21 '17 at 9:27
  • Didn't get you mean! You mean myserver:~/dir/2/ resulting from a script as its output and it's /to/ooo/ooo/oooo/looooong/path that you don't want to type in rsync?! Then PSkocik's answer is better choice` (surly I cannot change your choice) – αғsнιη Oct 21 '17 at 9:31
  • Yeah and I have variables that I can't even keep track of anymore. – Lumify Oct 21 '17 at 9:37
up vote 4 down vote accepted

You can use xargs or exactly this requirement. You can use the -I as place-holder for the input received from the pipeline, do

echo "myserver:${HOME}/dir/2/" | xargs -I {} rsync -r "{}" /local/path/

(or) use ~ without double-quotes under which it does not expand to the HOME directory path.

echo myserver:~/dir/2/ | xargs -I {} rsync -r "{}" /local/path/
  • xargs seems overkill for something where simple command substitution ($()) will do... – marcelm Oct 21 '17 at 10:04
  • 1
    @marcelm : Thanks for the feedback. Yes I just wanted to show one of the many options possible to do. It is up to the OP to choose the best option. Am sure it didn’t deserve a -ve vote – Inian Oct 21 '17 at 10:13

This

 rsync -r "$(echo myserver:~/dir/2/)" /local/path/

is the easiest way to do it.

Piping connects stdouts with stdins. Here you want the output to go to an argument, so you need something else than classical piping.

That something is command substitution ($()).

You may give rsync a list of files to download in a file or on standard input by using --files-from:

echo "dir/2/" | rsync --files-from=- -r user@server: /local/path/

The - makes rsync read from standard input. The server can't be given to rsync in this manner. There will only be a single connection made to the server and all files will be transferred over that connection.

If you use -a or --archive with --files-from then you need to explicitly add -r or --recursive if you want to recursion since that option, even though it's part of -a, is disabled when --files-from is used.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.