rsync exclude a directory but include a subdirectory

user1036651 picture user1036651 · Nov 25, 2011 · Viewed 18.8k times · Source

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

project/specs/install/project1

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/ \
     [email protected]:~/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

spider picture spider · Nov 25, 2011

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/ \
    [email protected]:~/projects/project

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

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

Then use the --filter option:

rsync -avz --delete --filter=". pattern.txt" \
    /srv/http/projects/project/ \
    [email protected]:~/projects/project

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