I would like to run
something > file
on a remote system through ssh, but if I run
ssh host something > file
the redirection is executed locally as ssh etc > file
I’ve tried it with ' or '' or dd or with a pipe | instead, but I can’t get it to work. How can this be done?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try:
ssh host 'something > file'
Here’s a contrived demonstration of a way to handle redirection, pipes and quotes:
ssh host date -d yesterday | awk "'{print $1}'" > 'file" "with spaces.out'
The pipe and redirection are escaped rather than being contained in an overall outer set of quotes, reducing the need to escape one level of quotes. The single quotes for the AWK command are protected by the double quotes that surround them. The filename could be protected in the same way, but here I show how the single quotes protect the double quotes and the escape.
Method 2
Even simpler, instead of:
ssh host something > file
do:
ssh host "something > file"
Method 3
The suggested solution works with pipes as well
ssh host 'some_command | some_other_command'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0