I am trying to read content on specific files. I have multiple hosts where these files are located. I am passing the commands below during ssh so that it can run and get the output.
When using the command below I get the specified errors. (its sshed to host but nothing happens)
$ ssh host "more $(find /my/path -name "test*")"
find: 0652-010 The starting directory is not valid.
When using exec its not working, (its sshed to host but nothing happens)
$ ssh host "find /my/path -name "test*" -exec more {} \;"`
When using xargs it is working as expected
$ ssh host "find /my/path -name "test*" | xargs more"
Can someone explain why method 1 & 2 are not working?
Please note that if I directly run command on remote host every method is working. for example all commands below are working as expected in remote host.
more $(find /my/path -name "test*")
find /my/path -name "test*" -exec more {} \;
find /my/path -name "test*" | xargs more
For (1) the "... $(command substitution)"
inside the "double-quote string" is being done by your local bash shell, you'll need to escape it so that that it seen & expanded until the command reaches the remote box:
ssh host "more \$(find /my/path -name 'test*')"
OR, because 'single quotes' don't expand $()
:
ssh host 'more $(find /my/path -name "test*")'
NOTE A - The above solutions are also careful to ensure that test*
is still quoted (single or double it doesn't matter) when reaches the remote box, because it's important that find
sees the exact string test*
, not what it might glob/*
/expand into IF there happened to be file names starting with test in the remote home dir.
For (2) similarly, you need to escape the \
so it is passed correctly to the remote box:
ssh host "find /my/path -name 'test*' -exec more {} \\;"
OR, because 'single quotes' don't -escape:
ssh host 'find /my/path -name "test*" -exec more {} \;'
For (3) be careful about NOTE A. When your (3) command reaches the remote host test*
is NOT quoted. Adding another layer or escaping of quotes will make it safer:
ssh host "find /my/path -name \"test*\" | xargs more"
OR
ssh host "find /my/path -name 'test*' | xargs more"
Caveat B - Programs like more
interact with your terminal/tty so you might need to force the remote sshd to allocate one with ssh -t host ...
Caveat C - All of the above assumes that your user's shell on the remote box is a "normal"/Bourne -type of shell (bash, dash, ash, sh, zsh). If remote user account has changed their login shell to something weird (tcsh, fish, ...) you would need to add another layer of quote-escaping to and invoke your commands in particular shell (bash f/ex): ssh host "/bin/bash -c \"more \\\$(find /my/path -name 'test*')\""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With