Search code examples
bashrsynccopying

Copying folders from file except subfolders from file


I want to copy some directories and files. I have made list of them (absolute path), but some of them contains folders I don't want (exclude folders also having absolute path). I want to copy only include and deeper (without files/folders mentioned in exclude) but this, and other versions I made does not seem to be running well.

rsync -rz  --exclude-from="$exclude" --include-from="$include" . $repository_path

enter image description here

Let say thats the file sturcture

file include has two rows:

/a
/b

file exclude:

/a/aa

And desired output:

enter image description here


Solution

  • Let's drill it out with your file structure. First of all we create testsync directory to play within:

    mkdir testsync && cd testsync
    mkdir -p a/aa/whatever b/bb c/whatever c/noneed
    touch a/file.txt a/picture.jpg b/bb/morefiles.txt b/txt.txt
    

    Lets check it out with tree:

    .
    ├── a
    │   ├── aa
    │   │   └── whatever
    │   ├── file.txt
    │   └── picture.jpg
    ├── b
    │   ├── bb
    │   │   └── morefiles.txt
    │   └── txt.txt
    └── c
        ├── noneed
        └── whatever
    

    We create include.dat file with contents:

    - /a/aa/*
    + /
    + /a
    + /a/***
    + /b
    + /b/***
    - *
    

    More about syntax of include/exclude file in manual (man rsync) in section INCLUDE/EXCLUDE PATTERN RULES.

    You HAVE TO include each directory with it's ancestor so it could be synced. That is why / and /a with /a/*** (triple asterisk for all directory contents within) is included

    We create another directory outside of testsync: targetsync and test:

    rsync -az --include-from=include.dat ~/testsync/ ~/targetsync/
    

    Now when we go to targetsync directory and use tree to get the picture:

    .
    ├── a
    │   ├── aa
    │   ├── file.txt
    │   └── picture.jpg
    └── b
        ├── bb
        │   └── morefiles.txt
        └── txt.txt