复杂的 rsync 过滤(包括和排除)

Complex rsync filtering (includes and excludes)

我得到的目录和文件看起来像这样

/1.txt
/2.txt
/3.txt
/20170407_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170407_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170408_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170408_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170409_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170409_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170410_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170410_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170411_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170411_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170412_000000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt
/20170412_120000
    /config
       file1.txt
    /logs
       file1.txt
    /nav
       file1.txt
    /schedule
       file1.txt
       file2.txt
       file3.txt
    /soh
       file1.txt
    /tracks
       file1.txt

我只想复制20170409_*/schedule、20170410_*/schedule、20170411_*/schedule文件夹和内容。

这主要有效:

 rsync -avhr --progress  --include="/20170409_*/schedule/" --include="/20170410_*/schedule/" --include="/20170411_*/schedule/"  --exclude="/*/*"  /source/dateTimeDirectories/ /destination/dateTimeDirectories/

但是这也会复制以下内容:

1.txt
2.txt
3.txt
20170407_000000/
20170407_120000/
20170408_000000/
20170408_120000/
...
20170412_000000/
20170412_120000/

如何省略我没有包含的文件和 dateTimeFolders,只得到我包含的 dateTimeFolder 的日程文件夹?

如果我理解正确的话,您需要在您的命令中添加选项--prune-empty-dirs(简称-m)。这些目录与排除其子项的排除规则不匹配,因此默认情况下会复制它们。 -m 阻止了这种情况。如果要防止其他文件被复制,则必须为它们添加特定的排除规则。例如,1.txt2.txt等文件可以被--exclude=/*.txt排除。总之,您的命令应如下所示:

 rsync -avhr \
   --progress \
   --prune-empty-dirs \
   --include="/20170409_*/schedule/" \
   --include="/20170410_*/schedule/" \
   --include="/20170411_*/schedule/" \
   --exclude="/*.txt" \
   --exclude="/*/*"  \
   /source/dateTimeDirectories/ \
   /destination/dateTimeDirectories/