I have requirement to connect to remote machine and then get info and pipe to while loop.

ssh rao@ "find ~/listfile/ -iname \"*log*\" |while read line; do cat $line; done"

The above commands is printing empty lines. I tried find itself to check command and it does works and list files.

ssh rao@ "find ~/listfile/ -iname \"*log*\""

How do I see the content of files found?

  • 1
    I would have escaped the $ in cat $line inside loop. – Archemar Sep 20 '17 at 14:42

You should use find alone if you are going to list files with matched name where you ssh in remote server and add only if files -type f.

ssh USER@HOST 'find ~/listfile/ -type f -iname "*log*" '

If you need to cat content of the files found, use find with cat as follows.

ssh USER@HOST 'find ~/listfile/ -type f -iname "*log*" -exec cat '{}' +' 
  • thanks for Quick answer. Here my requirement is to find the files in remote machine and display the content of the files. i.e the reason i am using the CAT command – venkata rao Sep 20 '17 at 5:21

I know it won't strictly answer your question but will surely solve your problem in a very classy way.

Instead of describing your command in the SSH line, write as a Bash function [1]:

function foo() { while true; do echo bar; done; } 

Then run your SSH command using declare builtin command [2] and then call your Bash function:

ssh example.com "$(declare -f foo); foo;"

You can simply write it directly on the command line or save in a file and use another Bash's builtin command, source.

Bang! Your can pretty much simplify your SSH line without having to copy a Shell script over the remote machine.

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.