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

后端 未结 4 1526
情话喂你
情话喂你 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:19

    If the first matching pattern excludes a directory, then all its descendants will never be traversed. When you want to include a deep directory e.g. company*/unique_folder1/** but exclude everything else *, you need to tell rsync to include all its ancestors too:

    rsync -r -v --dry-run                       \
        --include='/'                           \
        --include='/company*/'                  \
        --include='/company*/unique_folder1/'   \
        --include='/company*/unique_folder1/**' \
        --exclude='*'
    

    You can use bash’s brace expansion to save some typing. After brace expansion, the following command is exactly the same as the previous one:

    rsync -r -v --dry-run --include=/{,'company*/'{,unique_folder1/{,'**'}}} --exclude='*'
    

提交回复
热议问题