A method to pre-seed ignore patterns for new folder

Problem

For an existing folder, if using #include patterns.txt to load more patterns, I simply ensure that file exists. However, when adding a new folder to syncthing, this file does not exist yet. Using #include patterns.txt will cause the folder to complain about this and stop syncing.

Background

As implied, rather than putting ignore patterns like desktop.ini directly inside .stignore, instead I create a file such as patterns.txt with the above and load it using #include patterns.txt. This is for portability purposes - it’s easier to update only one file and reference it rather than edit .stignore all the time, especially when it is long.

Previous workaround

One can pre-create the folder, and pre-create patterns.txt, so that it is available when adding it through syncthing. However this requires manual intervention outside of the web GUI, which is difficult for someone using syncthing on a headless server who uses mostly the web GUI.

My solution

  1. Following the above, use a single folder, such as .stignore.d to store any pattern files to #include
  2. Pattern files can use any filename, I use .txt so they are openable in text editors
  3. For existing folders, the contents of .stignore may be as such:
#include /.stignore.d/patterns.txt

(/.stignore.d/... rather than .stignore.d/... - this ensures that no unnecessary folders are traversed by the watcher to look for patterns.txt)

  1. Key step - on new folders, add the folders with the following
/.stignore.d
*

This tells syncthing to only sync this folder and ignore everything else. The bonus is that even if this folder, or any pattern files within do not exist (yet), syncthing will not complain. This opens up a lot of possibilities.

  1. On other devices, create this folder, and place pattern files within (if not done already). The contents of .stignore.d and no other files will now be synced to the new device.
  2. On the new device, modify #stignore to use the patterns:
/.stignore.d
#include /.stignore.d/patterns.txt <- add this line
*

The file needs to exist at this point otherwise syncthing will complain, but that’s the point of the previous step - pull the pattern files from other devices.

  1. Now, remove the * at the end, to resume normal syncing of other files.
/.stignore.d
#include /.stignore.d/patterns.txt

Result

For me, this solves two problems I face when adding folders to a new device

  1. I do not need to worry about pre-creating the patterns file
  2. I do not need to “first sync, then ignore” - this guarantees that when the pattern file is loaded during step (6), the folder is empty.

A practical example - let’s say a folder contains movies and photos. Other devices sync both, this new device should sync only photos. If I sync the folder first, then add patterns later, I’ll end up with both, and need to remove movies after ignoring it. This way, movies is never synced at all.

This does not do what you describe. It ignores the folder .stignore.d, and then additionally everything else. You need to prefix the “exemption” with a ! character.

Also not sure why you want that extra leading slash in the include directive. AFAIK it does not change anything.

You’re right, that was a typo. As to the leading slash, the following is from the docs:

Negated patterns that can match items below the folder root will cause Syncthing to traverse otherwise ignored directories […]

As a special case, top-level rooted patterns (e.g. !/foo) do not cause this behaviour […]

While it is describing the behaviour for ignore patterns, I thought there would be no harm in assuming it would be the same for include directives too.

(because I use the same format for Android devices - minimizing unnecessary filesystem activity and thus preserving battery life is the goal.)

The described behavior has nothing to do with include directives. You can safely remove the leading slash there.