user1036651 user1036651 - 1 year ago 142
Bash Question

rsync exclude a directory but include a subdirectory

I am trying to copy a project to my server with rsync.
I have project specific install scripts in a subdirectory


What I am trying to do is exclude everything in the project/specs directory but the project specific install directory: project/specs/install/project1.

rsync -avz --delete --include=specs/install/project1 \
--exclude=specs/* /srv/http/projects/project/ \

But like this the content of the specs directory gets excluded but the install/project1 directory does not get included.

I have tried everything but i just don't seem to get this to work

Answer Source

Sometime it's just a detail.

Just change your include pattern adding a trailing / at the end of include pattern and it'll work:

rsync -avz --delete --include=specs/install/project1/ \
    --exclude=specs/* /srv/http/projects/project/ \

Or, in alternative, prepare a filter file like this:

$ cat << EOF >pattern.txt
> + specs/install/project1/
> - specs/*

Then use the --filter option:

rsync -avz --delete --filter=". pattern.txt" \
    /srv/http/projects/project/ \

For further info go to the FILTER RULES section in the rsync(1) manual page.