2

I'm trying to search for the file of the format : "installation_xx_int_${type}_*log" of the current date inside the remote server using below command:

ssh user@hostname 'file=$(find ~ -type f -name "installation_xx_int_${type}_*log" -mtime -1 -exec basename "{}" \;); cat "$file"' > ~/$file

but, the problem here is that the variables: ${type} and $file are not getting expanded inside ssh command , therefore unable to redirect the output of the file from remote server to my server.

If there's any other method to achieve this in more simpler way, please let us know!

Many thanks in advance!

3

Shell variables are not magically passed from client to server over ssh. Some systems accept passing environment variables as long as their name starts with LC_ (look for AcceptEnv LC_* in they sshd_config), so you could do:

LC_type="$type" ssh -o SendEnv=LC_type user@hostname '
  file=$(
    find ~ -type f -name "installation_xx_int_${LC_type}_*log" -mtime -1 \
      -exec basename "{}" \;
  )
  cat -- "$file"
' > ~/test.log

If $type doesn't contain characters that are special within double quotes in the syntax of the remote shell (", `, $, \ if the remote shell is POSIX-like), you could have the local shell expand it in the code argument to ssh:

ssh user@hostname '
  file=$(
    find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 \
      -exec basename "{}" \;
  )
  cat -- "$file"
' > ~/test.log

For that expansion to happen, we need to get out of the single quotes (for the local shell) and into double quotes.

In all those, if $type contains wildcards (*, ?, \, [...]), they will be treated as such by find's -name which takes a pattern, and you'd need to escape them with \ for them to be taken literally.

It's not clear to me why you're taking the basename and not just doing:

ssh user@hostname '
  find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 \
    -exec cat {} +
' > ~/test.log

If, as clarified in the later edit to your question, you want to store the contents of the remote files into files by the same name on the local system, you'd do something like (assuming those are GNU systems, though you should be able to do something similar with pax, star (which can also do the find part by itself) or libarchive bsdtar):

ssh user@hostname '
  find ~ -type f -name "installation_xx_int_'"$type"'_*log" -mtime -1 -print0 |
    tar --null -T - -cf - |
    gzip -1' | tar --transform='s:.*/::' -zxf -

That is, use the tar archive format to store both file path and contents (and the --transform on the receiving tar on the local machine, strips off the directory part).

2
  • Many thanks for the answer! I want to capture the filename into a variable so that the same variable can be used in the output file name , like this: ssh user@hostname 'file=$(find ~ -type f -name "installation_xx_int_'"${type}"'_*log" -mtime -1 -exec basename "{}" \;); cat '"$file"'' > ~/$file, but now i can see that "$file" in cat '"$file"' is not getting expanded!!
    – User123
    Sep 27 at 15:35
  • @User123, See edit, that would be the reverse problem: pass a variable from remote shell to local shell. Better to use tar / pax / cpio... for that. Sep 27 at 16:21

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.