0

I want to run a script that deletes a files on computer and copies over another file from a connected host using scp command

Here is the script:

#!/bin/bash
echo "Moving Production Folder Over"
cd ~
sudo rm -r Production
scp -r host@192.168.123.456:/home/user1/Documents/Production/file1 /home/user2/Production

I would want to cd into the Production directory after it is copied over. How can I go about this? Thanks!

  • You seem to misunderstand how commands work? `bash` runs each command one at a time (unless you manually parallelize). If there's a command after `scp`, that command won't run until the `scp` completes. If `scp` is waiting for user input, `scp` hasn't completed; and the next command wont run. – omajid Dec 22 '22 at 15:52
  • Btw, you might want to look into `rsync` if you want to sync files between machines. https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories might be a starting point. https://stackoverflow.com/questions/20244585/how-does-scp-differ-from-rsync is also worth reading – omajid Dec 22 '22 at 15:55
  • il look into rsync, thanks! – stopbanningmelmao123 Dec 22 '22 at 16:23
  • you should use ssh-keys. Then you do not need a password for your ssh connection any more. – Oliver Gaida Dec 22 '22 at 18:08
  • And take into account that if you `cd` inside the script it won't affect the parent shell – Diego Torres Milano Dec 22 '22 at 19:49

0 Answers0