Is it possible to do this:
ssh user@socket command /path/to/file/on/local/machine
That is to say, I want to execute a remote command using a local file in one step, without first using scp
to copy the file.
Is it possible to do this:
ssh user@socket command /path/to/file/on/local/machine
That is to say, I want to execute a remote command using a local file in one step, without first using scp
to copy the file.
You missed just one symbol =)
ssh user@socket command < /path/to/file/on/local/machine
/dev/stdin
or -
. May or may not work (/dev/stdin
is a file, but seeking it will fail)
– derobert
Oct 26 '12 at 16:03
One way that works regardless of the command is to make the file available on the remote machine via a remote filesystem. Since you have an SSH connection:
<
since you thought writing was required (as was implied by the original title) but you were simply offering another way of doing it (which just happens to allow writing as a side effect).
– iconoclast
Aug 01 '14 at 17:25
# What if remote command can only take a file argument and not read from stdin? (1_CR)
ssh user@socket command < /path/to/file/on/local/machine
...
cat test.file | ssh user@machine 'bash -c "wc -l <(cat -)"' # 1_CR
As an alternative to bash
process substitution <(cat -)
or < <(xargs -0 -n 1000 cat)
(see below) you can just use xargs
and cat
to pipe the contents of the specified files to wc -l
(which is more portable).
# Assuming that test.file contains file paths each delimited by an ASCII NUL character \0
# and that we are to count all those lines in all those files (provided by test.file).
#find . -type f -print0 > test.file
# test with repeated line count of ~/.bash_history file
for n in {1..1000}; do printf '%s\000' "${HOME}/.bash_history"; done > test.file
# xargs & cat
ssh localhost 'export LC_ALL=C; xargs -0 -n 1000 cat | wc -l' <test.file
# Bash process substitution
cat test.file | ssh localhost 'bash -c "export LC_ALL=C; wc -l < <(xargs -0 -n 1000 cat)"'