I'd be really useful if I could do something like this:

  1. go in a local dir
  2. SSH to a remote host
  3. Local dir gets synced to the remote host (local -> remote)
  4. run command remotely
  5. again sync with the results (remote -> local)

Is there a way to do this with standard tools like ssh or an application that does it?

  • Almost sounds like you're asking for a checkout/checking process like Subversion or git does – warren Jun 15 '17 at 16:42
  • Have you tried using SSH to do this? What happens when you try? Right now your question is requesting that we recommend a software product to do this, and recommendation questions are off-topic here on Super User because software use is very much a matter of preference, and software that works today may not work tomorrow. Removing the recommendation portion of your question and focusing on the problem and desired solution would make this a good question. – music2myear Jun 19 '17 at 21:05

You could look at sshfs - mounting a directory via SSH rather than transferring the files back and forth.

In this case you would connect to the server and mount your PC's directory. You could then operate directly on the files, before unmounting and disconnecting.

Depending on the network, you might need to forward your local SSH server's listening port.

It's FUSE so there is no need for root / sudo.

Mount with:

sshfs user@host:/home/myuser/mydir ./mydir

Unmount with:

fusermount -u ./mydir
| improve this answer | |

You can use rsync --rsh=ssh for steps 2+3, ssh for step 4, and rsync again for step 2+5.

The parameters for rsync might appear a little complex at first - but you can always test with --dry-run first.

| improve this answer | |
  • 1
    Worth noting that if the src / dest arguments are a directory, you might get unexpected behaviour - I always append a trailing / to directories to get consistent behaviour that makes sense to me. – Attie Jun 15 '17 at 15:49
  • ... also worth providing an example of the standard rsync -av ${SRC} ${DEST} usage. – Attie Jun 15 '17 at 15:55
  • 1
    @Attie I was reluctant to add that exactly because of the issues with directories you mentioned, and to discourage copy-paste from this website... noone should ever do that when it comes to syncing files :) – jvb Jun 15 '17 at 15:58
  • Fair enough :-) – Attie Jun 15 '17 at 15:59

For the syncing part(s) of your list – since you want to transfer bidirectionally – you may also have a look at unison, which is similar but different to rsync. You may can run it before and after your command and it will keep both sides in sync.

Similar to rsync:

  • transport via SSH (as well as local and others)
  • differential transfer under the hood, if parts of the files are the same, they do not need to be transmitted completely, but only the changed (or different) parts are transfered and the whole file is reconstructed on the other side, very useful to reduce bandwidth usage

Different to rsync:

  • needs unsion on both ends – and it is very picky about the unison version number
  • bidirectional sync: syncs files changed since the last sync on one end to the other end – and it detects if both were changed and either ignores it or lets you choose
  • a textui and gui for interactive use is available as well as a non-interactive mode

If you want to make the copying direction explicit, I would also suggest rsync, as did jvb in https://superuser.com/a/1219619/738892 – also starting with --dry-run (or -n for short) is very helpful. Atties comment about the slashes is important, too: start with -av for copying the directory (with output – to see what happens).

You may or may not want to use --del and friends, depending on whether you want to delete files which vanished from the SRC in the DEST. Depending on your use case, --update (skips files that are newer on DEST), may be useful to you.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.