copying a directory from one location to another in the same server but the script to copy is present in another server

Solution 1:

To complete the task, you can use the ssh command in a way as this:

ssh user@host 'cp -R /source/dir /dest/dir'

Thus instead of opening a login shell the ssh client will execute the command within the quote marks on the remote system and then will close the connection. In this case the remote system will process the data. In addition you can capture the output of the command and save it as a local file if you need, for example:

ssh user@host '<remote command>' > /local/path/file.log
ssh user@host '<remote command> 2>&1' > /path/file.log  # capture the remote errors