2

I am trying to write a script in a server which copies a directory present in another server and paste it in the same directory but at different location, what is the correct format for the SCP command for this process.

6
  • "same directory but different location" - is it another sub directory in the same server or the same directory on another server ?
    – CrazyTux
    Sep 6, 2021 at 7:21
  • 2
    Instead of scp you can use the ssh command in this way: ssh user@host 'cp -R /dest/dir /target/dir'
    – pa4080
    Sep 6, 2021 at 7:24
  • another sub directory in the same server Sep 6, 2021 at 8:19
  • 1
    Show us what you tried thus far: that may render your specific problem more clearly. With scp, you can specify source and destination on remote hosts, so I do not see your specific problem as long as you specify correct pathnames to the remote host.
    – vanadium
    Sep 6, 2021 at 8:52
  • check out this guide shellhacks.com/ssh-execute-remote-command-script-linux
    – CrazyTux
    Sep 6, 2021 at 9:09

1 Answer 1

4

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

You must log in to answer this question.

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