my rsync command looks like this:

rsync --update -rav --filter="merge $HOME/mergefilter.txt" ~/ (remote_destination):~/

My mergefilter.txt file looks like this:

+ /directory_1/***
+ /directory_2/***
+ /directory_etc/***
- /**

The rsync command works great and updates everything in the specified directory trees. However, I am looking to add some specifics to it. I am not sure of the necessary syntax, but for one of my directory trees, I want to only include certain files. For example, imagine my mergefilter.txt working like this:

+ /directory_1/***
+ /directory_2/*** --include="*.c++" --include="*.h" 
+ /directory_etc/***
- /**

So that when syncing the directory_2 directory tree (and its subdirectories), only .c++ and .h files are updated.

I have viewed other stackexchange pages asking similar questions, but none of them demonstrate how to use include when using a filter file. What would be the correct syntax here?

1 Answer 1


You are already using include; each line that begins with + is an include rule; every line that begins with - is an exclude rule.

If you want to include only specific files within a certain directory, then you have to spell it out unfortunately. Your filter file becomes:

+ /dir_1/***
+ /dir_2/
+ /dir_2/**.c++
+ /dir_2/**.h
+ /dir_2/**/
- /dir_2/**
+ /dir_etc/***
- /*

First you have to ensure that dir_2 is included: + /dir_2/.

Next you include all *.c++ and all *.h files within that dir_2; remember that ** matches all characters including slash.

Next you also have to include all directories within dir_2: + /dir_2/**/ otherwise the files within subdirectories aren't found, as those subdirectories are excluded.

Finally exclude all other files.

Note that you can also use per-directory merge files, so rsync will check every directory for a given filename and if found, use that file as an additional filter for that directory. See the -F option in the manpage.

  • Can you please explain why you used - /*at the end? What is the difference between - /* and - *? Cheers. Jan 7, 2021 at 20:51
  • @FlexMcMurphy at that point we only want to exclude files and directories in the root that haven't already explicitly included.
    – wurtel
    Jan 12, 2021 at 11:30
  • Yes but wouldn't - * achieve the same result? With the -a parameter to rsync it will recurse through every sub-directory of the source dir and - * will delete everything that has not been explicitly included? Jan 13, 2021 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .