How to use Rsync to copy only specific subdirectories (same names in several directories)

后端 未结 4 1523
情话喂你
情话喂你 2021-01-31 08:36

I have such directories structure on server 1:

  • data
    • company1
      • unique_folder1
      • other_folder
      • ...
    • company2
4条回答
  •  温柔的废话
    2021-01-31 09:00

    For example, if you only want to sync target/classes/ and target/lib/ to a remote system, do

    rsync -vaH --delete --delete-excluded --include='classes/***' --include='lib/***' \
          --exclude='*' target/ user@host:/deploy/path/
    

    The important things to watch:

    • Don't forget the "/" from the end of the pathes, or you will get a copy into subdirectory.
    • The order of the --include, --exclude counts.
    • Contrary the other answers, starting with "/" an include/exclude parameter is unneeded, they will automatically appended to the source directory (target/ in the example).
    • To test, what exactly will happen, we can use a --dry-run flags, as the other answers say.
    • --delete-excluded will delete all content in the target directory, except the subdirectories we specifically included. It should be used wisely! On this reason, a --delete is not enough, it does not deletes the excluded files on the remote side by default (every other, yes), it should be given beside the ordinary --delete, again.

提交回复
热议问题