Mark_Anderson
Mark_Anderson

Reputation: 1324

rsync: How to NOT apply wildcard excludes recursively

I'm using rsync to do a local backup of a Matlab model (end goal is a shell script to autobackup my key PhD files).

The model has layers of folders, and I want to exclude some wildcard matches on top level but not lower levels. Is this possible?

Example file structure:

/Model               % DO NOT WANT to sync *.mat files in this directory
/Model/Data/Greens   % WANT to sync *.mat files in this subdirectory

Example (simplified) code:

#!/bin/bash
rsync -a --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup

The example code excludes ALL *.mat files in ALL folder levels. How do I fix it to only apply the '*.mat' wildcard to the top folder?

TL;DR: Is there a way to apply rsync recursively to all subfolders, but apply a wildcard exclude to only the top-level folder?

Upvotes: 2

Views: 1339

Answers (1)

Suever
Suever

Reputation: 65460

By default, *.mat is going to match all .mat files in your folder and all sub-directories. If you'd only like to ignore .mat files in the parent directory, you can add the Model/ prefix to the exclude directive to only ignore .mat files in that folder

rsync -a --exclude="Model/*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup

Alternately, if you want to include .mat files only in the Model/Data/Greens folder you can use the --include option in conjunction with the --exclude option

rsync -a --include="Model/Data/Greens/*.mat" --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup

Upvotes: 3

Related Questions