I need to basically copy only the last created folder in a directory of many folders within an SSH session. I have the correct find command to do this, but I just can't seem to store the result within the SSH session. the LATEST_FOLDER variable is empty, and I've tried something like this also:

cp -f arkOutput/$(find . -type d -exec stat --printf="%Y\t%n\n" {} \; | sort -n -r | head -1 | cut -f2 | cut -c 3-)/* /synology/data/${OUTPUT_PATH}/${i}/ark

Here is the full ssh session code:

    ssh [email protected] << EOF
      cd /data/dev/${ARK_PATH}/
      ./gradlew bootRun --no-daemon;
      LATEST_FOLDER=$(find . -type d -exec stat --printf="%Y\t%n\n" {} \; | sort -n -r | head -1 | cut -f2 | cut -c 3-);
      cp -f arkOutput/${LATEST_FOLDER}/* /synology/data/${OUTPUT_PATH}/${i}/ark

the /data/dev dir only exists on the server I'm sshing into, so I can't really get that before hand and pass it in. the ./gradlew bootRun command creates the folder I'm interested in

  • hello, so that worked for the ssh local variable LATEST_FOLDER, however the ${ARK_PATH} and ${OUTPUT_PATH} are now no longer recognized. They were defined outside of the ssh session, earlier in the script. is it possible to still have those be defined within the SSH session when I do \EOF (like you suggested)? Commented Mar 22, 2023 at 3:54
  • 1
    The point being made was that the things in the HERE document are being evaluated. To stop the evaluation of the whole thing you can quote at least one character of the delimiter. You appear to be saying that you want ome of the document to be evaluated, but not all. In that case you need to quote the bits you don't want evaluated.
    – icarus
    Commented Mar 22, 2023 at 5:50
  • @dave_thompson_085 please don't post answers in comments. Comments cannot be voted on so this circumvents the normal quality control processes of the site, and they can also discourage others from posting actual answers.
    – terdon
    Commented Mar 22, 2023 at 12:04

1 Answer 1


Fixing up the current way of doing things

ssh [email protected] << EOF
   cd /data/dev/${ARK_PATH}/
   ./gradlew bootRun --no-daemon
   LATEST_FOLDER=\$(find . -type d -exec stat --printf="%Y\t%n\n" {} \; | sort -n -r | head -1 | cut -f2 | cut -c 3-)
   cp -f arkOutput/\${LATEST_FOLDER}/* /synology/data/${OUTPUT_PATH}/${i}/ark

I am not sure if you want ${i} or \${i} - I suspect the former.

Second way

A different approach would be to split things into 2 or 3 ssh commands. E.G.

ssh "[email protected]" "cd /data/dev/${ARK_PATH}/ && ./gradlew bootRun --no-daemon"
LATEST_FOLDER=$(ssh "[email protected]" "find /data/dev/${ARK_PATH}/ -type d -exec stat --printf='%Y\t%n\n' {} + | sort -n -r | head -1 | cut -f2 | cut -c 3-" )
ssh "[email protected]" "cp -f /data/dev/${ARK_PATH}/arkOutput/${LATEST_FOLDER}/* /synology/data/${OUTPUT_PATH}/${i}/ark"

This might be a little more obvious.

Third way

A third approach might be to just put the script on the server, and just pass the parameters to it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .