austingray
austingray

Reputation: 10547

rsync - exclude everything except a subdir, but exclude a sub subdir

I'm attempting to rsync a remote directory to local, I want to exclude everything except a subdirectory. But that subdirectory has a subdirectory inside of it that i want to exclude. My understanding is rsync applies the include after the exclude, I'm trying to figure out the necessary pattern here. These are the include/exclude flags I am passing:

rsync -Pav -e "ssh -i /path/to/key.pem" \
  --include="subdir/***" \
  --exclude="subdir/subsubdir" \
  --exclude="*" \
  [email protected]:/path/to/remote/dir/ \
  /path/to/local/dir/

Edit: This currently results with rsync grabbing everything inside of subdir, including the subsubdir that i am trying to exclude.

Thank you!

Upvotes: 4

Views: 2555

Answers (1)

austingray
austingray

Reputation: 10547

My assumption on how rsync applied filters was misinformed. @meuh left a comment that was the correct answer:

rsync applies the filters in the order you give them, so try the subsub exclude before the include.

I also had to add a trailing slash to my exclude. Here is the correct rsync command:

rsync -Pav -e "ssh -i /path/to/key.pem" \
  --exclude="subdir/subsubdir/" \
  --include="subdir/***" \
  --exclude="*" \
  [email protected]:/path/to/remote/dir/ \
  /path/to/local/dir/

Here is a demonstration of the command running locally:

rsync -Pav \
  --exclude="subdir/subsubdir/"
  --include="subdir/***" \
  --exclude="*" \
  ./remote/ \
  ./local/

Before:

.
├── local
└── remote
    ├── subdir
    │   ├── subsubdir
    │   │   └── test.txt
    │   └── test.txt
    └── test.txt

After:

.
├── local
│   └── subdir
│       └── test.txt
└── remote
    ├── subdir
    │   ├── subsubdir
    │   │   └── test.txt
    │   └── test.txt
    └── test.txt

Upvotes: 4

Related Questions