Search code examples
linuxunixrsync

rsync only directories that exist?


On my local machine I have several plugins, and on the server I have a few.

Local Machine:

~/wp-content/plugins/plugin1
~/wp-content/plugins/plugin1/includes
~/wp-content/plugins/plugin2
~/wp-content/plugins/plugin3
~/wp-content/plugins/plugin4
~/wp-content/plugins/plugin5

Remote Machine:

~/wp-content/plugins/plugin1
~/wp-content/plugins/plugin1/includes
~/wp-content/plugins/plugin3
~/wp-content/plugins/plugin5

What rsync command can I use to update all the files in the remote directories, but only if they exist? For this I would like to have plugin1, plugin1/includes, plugin3, and plugin5 synced - with all files and directories inside - but not plugin2 or plugin4. Basically, update all of the plugins if they exist with a single rsync command.


Solution

  • This is not completely possible with only one rsync command. If you only want to update the remote files, but do not want to add new files, you can use this command:

    rsync -rP --existing source/ user@remote:target/
    

    This will not create new files or directories at all, but update differing ones.

    Edit: Maybe you could do something like this (assuming GNU find, if BSD/OS X: replace maxdepth with depth):

    #!/bin/bash
    REMOTE_TARGET_DIR=/some/path
    REMOTE_DIRS=$(ssh user@remote "find $REMOTE_TARGET_DIR -maxdepth 1 -type d -printf '%P\n' | sed 1d"
    for DIR in $REMOTE_DIRS
    do
      rsync -rP "$DIR" "user@remote:$REMOTE_TARGET_DIR/"
    done
    

    Warning: I have not tested this, you might want to add a "-n" to rsync (dry run) to see what happens.