borg-patterns(1)
π NAME π
borg-patterns β bestie, it's the borg backup tool's deep-dive page on patterns β¨ (this is borg-patterns(1), and yeah it's a whole thing).
β¨ DESCRIPTION β¨
okay so. The path/filenames that get handed to pattern matching start with the recursion root that's currently active. You normally pass the recursion root(s) when you run borg, and they can be relative or absolute paths β both are valid, no drama π
Be careful though, because your patterns have to match the archived paths, not the fancy paths you typed:
- Archived paths never start with a leading slash (
/), nor with., nor with..π«Ά- When you back up absolute paths like
/home/user, the archived paths start withhome/user. - When you back up relative paths like
./src, the archived paths start withsrc. - When you back up relative paths like
../../src, the archived paths start withsrc.
- When you back up absolute paths like
- When you use the slashdot hack, patterns match against the unstripped path, i.e., when you back up
/this/gets/stripped/./this/gets/archived, patterns must matchthis/gets/stripped/this/gets/archived.
A directory exclusion pattern can end with or without a slash (/), and it genuinely matters:
- ends with a slash, like
some/path/β the directory itself is included, but its content is not π«£ - no slash, like
some/pathβ both the directory and its content get excluded π
Borg supports a bunch of different pattern styles. To pick a non-default style for one specific pattern, slap two characters and a colon in front of it β like fm:path/* or sh:path/** π
The default pattern style for --exclude is different from --pattern β more on that below, iykyk.
πΈ Fnmatch, selector fm: πΈ
This is the default style for --exclude and --exclude-from. Fnmatch lives over here: https://docs.python.org/3/library/fnmatch.html.
These patterns use a variant of shell pattern syntax, where:
*matches any number of characters?matches any single character[...]matches any single character specified, including ranges[!...]matches any character not specified
For the purpose of these patterns, the path separator (backslash for Windows and / on other systems) is not treated specially. Wrap meta-characters in brackets for a literal match (i.e. [?] to match the literal character ?). For a path to match a pattern, the full path must match, or it must match from the start of the full path to just before a path separator. Except for the root path, paths will never end in the path separator when matching is attempted. Thus, if a given pattern ends in a path separator, a * is appended before matching is attempted. A leading path separator is always removed.
π¦ Shell-style patterns, selector sh: π¦
This is the default style for --pattern and --patterns-from β¨
Like fnmatch patterns, these are similar to shell patterns. The difference is that the pattern may include **/ for matching zero or more directory levels, and * for matching zero or more arbitrary characters with the exception of any path separator. A leading path separator is always removed.
π Regular expressions, selector re: π
Regular expressions similar to those found in Perl are supported. Unlike shell patterns, regular expressions are not required to match the full path, and any substring match is sufficient. It is strongly recommended to anchor patterns to the start (^), to the end ($) or both. Path separators (backslash for Windows and / on other systems) in paths are always normalized to a forward slash (/) before applying a pattern. The regular expression syntax is described in the Python documentation for the re module: https://docs.python.org/3/library/re.html.
π Path prefix, selector pp: π
This pattern style is useful to match whole sub-directories. The pattern pp:root/somedir matches root/somedir and everything therein. A leading path separator is always removed.
π Path full-match, selector pf: π
This pattern style is (only) useful to match full paths. This is kind of a pseudo pattern as it can not have any variable or unspecified parts β the full path must be given. pf:root/file.ext matches root/file.ext only. A leading path separator is always removed.
Implementation note, and it's giving iconic: this is implemented via very time-efficient O(1) hashtable lookups (this means you can have huge amounts of such patterns without impacting performance much). Due to that, this kind of pattern does not respect any context or order. If you use such a pattern to include a file, it will always be included (if the directory recursion encounters it). Other include/exclude patterns that would normally match will be ignored. Same logic applies for exclude.
Note:re:,sh:andfm:patterns are all implemented on top of the Python SRE engine. It is very easy to formulate patterns for each of these types which requires an inordinate amount of time to match paths. If untrusted users are able to supply patterns, ensure they cannot supplyre:patterns. Further, ensure thatsh:andfm:patterns only contain a handful of wildcards at most. π¨ (no cap, this one is a real safety thing)
Exclusions can be passed via the command line option --exclude. When used from within a shell, the patterns should be quoted to protect them from expansion π
Patterns matching special characters, e.g. white space, within a shell may require adjustments, such as putting quotation marks around the arguments. Example: using bash, the following command line option would match and exclude "item name":
--pattern='-path/item name'
Note that when patterns are used within a pattern file directly read by borg, e.g. when using --exclude-from or --patterns-from, there is no shell involved and thus no quotation marks are required. π
The --exclude-from option permits loading exclusion patterns from a text file with one pattern per line. Lines empty or starting with the number sign (#) after removing whitespace on both ends are ignored. The optional style selector prefix is also supported for patterns loaded from a file. Due to whitespace removal, paths with whitespace at the beginning or end can only be excluded using regular expressions.
To test your exclusion patterns without performing an actual backup you can run borg create --list --dry-run ... β vibe check first, back up later π
Examples:
# Exclude a directory anywhere in the tree named ``steamapps/common``
# (and everything below it), regardless of where it appears:
$ borg create -e 'sh:**/steamapps/common/**' backup /
# Exclude the contents of ``/home/user/.cache``:
$ borg create -e 'sh:home/user/.cache/**' backup /home/user
$ borg create -e home/user/.cache/ backup /home/user
# The file '/home/user/.cache/important' is *not* backed up:
$ borg create -e home/user/.cache/ backup / /home/user/.cache/important
# Exclude '/home/user/file.o' but not '/home/user/file.odt':
$ borg create -e '*.o' backup /
# Exclude '/home/user/junk' and '/home/user/subdir/junk' but
# not '/home/user/importantjunk' or '/etc/junk':
$ borg create -e 'home/*/junk' backup /
# The contents of directories in '/home' are not backed up when their name
# ends in '.tmp'
$ borg create --exclude 're:^home/[^/]+\.tmp/' backup /
# Load exclusions from file
$ cat >exclude.txt <<EOF
# Comment line
home/*/junk
*.tmp
fm:aa:something/*
re:^home/[^/]+\.tmp/
sh:home/*/.thumbnails
# Example with spaces, no need to escape as it is processed by borg
some file with spaces.txt
EOF
$ borg create --exclude-from exclude.txt backup /
A more general and easier to use way to define filename matching patterns exists with the --pattern and --patterns-from options. Using these, you may specify the backup roots, default pattern styles and patterns for inclusion and exclusion π
π Root path prefix R π
A recursion root path starts with the prefix R, followed by a path (a plain path, not a file pattern). Use this prefix to have the root paths in the patterns file rather than as command line arguments.
π Pattern style prefix P (only useful within patterns files) π
To change the default pattern style, use the P prefix, followed by the pattern style abbreviation (fm, pf, pp, re, sh). All patterns following this line in the same patterns file will use this style until another style is specified or the end of the file is reached. When the current patterns file is finished, the default pattern style will reset.
π Exclude pattern prefix - π
Use the prefix -, followed by a pattern, to define an exclusion. This has the same effect as the --exclude option.
π Exclude no-recurse pattern prefix ! π
Use the prefix !, followed by a pattern, to define an exclusion that does not recurse into subdirectories. This saves time, but prevents include patterns to match any files in subdirectories.
β Include pattern prefix + β
Use the prefix +, followed by a pattern, to define inclusions. This is useful to include paths that are covered in an exclude pattern and would otherwise not be backed up.
Note: Via--patternor--patterns-fromyou can define BOTH inclusion and exclusion of files using pattern prefixes+and-. With--excludeand--exclude-fromONLY excludes are defined.
The first matching pattern is used, so if an include pattern matches before an exclude pattern, the file is backed up. Note that a no-recurse exclude stops examination of subdirectories so that potential includes will not match β use normal excludes for such use cases.
Example:
# Define the recursion root
R /
# Exclude all iso files in any directory
- **/*.iso
# Explicitly include all inside etc and root
+ etc/**
+ root/**
# Exclude a specific directory under each user's home directories
- home/*/.cache
# Explicitly include everything in /home
+ home/**
# Explicitly exclude some directories without recursing into them
! re:^(dev|proc|run|sys|tmp)
# Exclude all other files and directories
# that are not specifically included earlier.
- **
Note: It's possible that a sub-directory/file is matched while parent directories are not. In that case, parent directories are not backed up thus their user, group, permission, etc. can not be restored. π
Note that the default pattern style for --pattern and --patterns-from is shell style (sh:), so those patterns behave similar to rsync include/exclude patterns. The pattern style can be set via the P prefix.
okay so here's the deal with the ORDER of everything β it literally decides who wins π
Command-line patterns from --pattern and excludes from --exclude get looked at FIRST, in the exact order you typed them. Then patterns from --patterns-from get added on. And exclusion patterns from --exclude-from files? They get appended last. So yes bestie, order matters, this is not a drill π«Ά
β¨ examples (slay) β¨
# backup pics, but not the ones from 2018, except the good ones:
# note: using = is essential to avoid cmdline argument parsing issues.
borg create --pattern=+pics/2018/good --pattern=-pics/2018 repo::arch pics
# use a file with patterns:
borg create --patterns-from patterns.lst repo::arch
and that patterns.lst file could look like this π
# "sh:" pattern style is the default, so the following line is not needed:
P sh
R /
# can be rebuild
- home/*/.cache
# they're downloads for a reason
- home/*/Downloads
# susan is a nice person
# include susans home
+ home/susan
# also back up this exact file
+ pf:home/bobby/specialfile.txt
# don't backup the other home directories
- home/*
# don't even look in /proc
! proc
πΈ recursion roots: pick your fighter πΈ
You can specify recursion roots either on the command line or in a patternfile β both are valid, it's giving options β¨
# these two commands do the same thing
borg create --exclude home/bobby/junk repo::arch /home/bobby /home/susan
borg create --patterns-from patternfile.lst repo::arch
And the patternfile, iconic behavior incoming:
# note that excludes use fm: by default and patternfiles use sh: by default.
# therefore, we need to specify fm: to have the same exact behavior.
P fm
R /home/bobby
R /home/susan
- home/bobby/junk
π read that note again bestie β excludes default tofm:and patternfiles default tosh:, so if you want them to vibe the same you gotta sayfm:out loud. no cap.
The whole point of this? You get to share the same patterns between multiple repositories without needing to type them out on the command line every single time π efficiency queen behavior.
π AUTHOR π
<h6>cute flourish aside, this is the credits section</h6>
The Borg Collective β π the whole crew, the icons, the moment π
and that's the page bestie! π¦ this was written by the Borg Collective, and this whole thing is from the version dated 2026-07-18 β that's the borg-patterns(1) manpage, mwah, go forth and pattern responsibly β¨π
π¦ where this page comes from
- alpine 3.24: borgbackup-doc 1.4.5-r0 Β· BSD-3-Clause
- arch rolling: borg 1.4.5-1 Β· BSD
- ubuntu stonking: borgbackup 1.4.5-1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms