Skip to content
Advertisement

Complex rsync filtering (includes and excludes)

I got directories and files that look like this

/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

I only want to copy the 20170409_*/schedule, 20170410_*/schedule, 20170411_*/schedule folders and contents.

This mostly works:

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

However this also copies the following:

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

How do I omit the files and the dateTimeFolders I haven’t included and only get the dateTimeFolder’s schedule folder that I have included?

Advertisement

Answer

If I understand the situation correctly, you need to add the option --prune-empty-dirs (in short, -m) to your command. Those directories are not matched by the exclude rule which exludes their children, so they are copied by default. -m prevents this. If you want to prevent other files to be copied, you must add specific exclude rules for them. For instance, the files 1.txt, 2.txt and so on can be excluded by --exclude=/*.txt. All in all, your command should look as follows:

 rsync -avhr 
   --progress 
   --prune-empty-dirs 
   --include="/20170409_*/schedule/" 
   --include="/20170410_*/schedule/" 
   --include="/20170411_*/schedule/" 
   --exclude="/*.txt" 
   --exclude="/*/*"  
   /source/dateTimeDirectories/ 
   /destination/dateTimeDirectories/
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement