github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/docs/content/filtering.md (about)

     1  ---
     2  title: "Filtering"
     3  description: "Filtering, includes and excludes"
     4  ---
     5  
     6  # Filtering, includes and excludes #
     7  
     8  Rclone has a sophisticated set of include and exclude rules. Some of
     9  these are based on patterns and some on other things like file size.
    10  
    11  The filters are applied for the `copy`, `sync`, `move`, `ls`, `lsl`,
    12  `md5sum`, `sha1sum`, `size`, `delete` and `check` operations.
    13  Note that `purge` does not obey the filters.
    14  
    15  Each path as it passes through rclone is matched against the include
    16  and exclude rules like `--include`, `--exclude`, `--include-from`,
    17  `--exclude-from`, `--filter`, or `--filter-from`. The simplest way to
    18  try them out is using the `ls` command, or `--dry-run` together with
    19  `-v`. `--filter-from`, `--exclude-from`, `--include-from`, `--files-from`,
    20  `--files-from-raw` understand `-` as a file name to mean read from standard
    21  input.
    22  
    23  ## Patterns ##
    24  
    25  The patterns used to match files for inclusion or exclusion are based
    26  on "file globs" as used by the unix shell.
    27  
    28  If the pattern starts with a `/` then it only matches at the top level
    29  of the directory tree, **relative to the root of the remote** (not
    30  necessarily the root of the local drive). If it doesn't start with `/`
    31  then it is matched starting at the **end of the path**, but it will
    32  only match a complete path element:
    33  
    34      file.jpg  - matches "file.jpg"
    35                - matches "directory/file.jpg"
    36                - doesn't match "afile.jpg"
    37                - doesn't match "directory/afile.jpg"
    38      /file.jpg - matches "file.jpg" in the root directory of the remote
    39                - doesn't match "afile.jpg"
    40                - doesn't match "directory/file.jpg"
    41  
    42  **Important** Note that you must use `/` in patterns and not `\` even
    43  if running on Windows.
    44  
    45  A `*` matches anything but not a `/`.
    46  
    47      *.jpg  - matches "file.jpg"
    48             - matches "directory/file.jpg"
    49             - doesn't match "file.jpg/something"
    50  
    51  Use `**` to match anything, including slashes (`/`).
    52  
    53      dir/** - matches "dir/file.jpg"
    54             - matches "dir/dir1/dir2/file.jpg"
    55             - doesn't match "directory/file.jpg"
    56             - doesn't match "adir/file.jpg"
    57  
    58  A `?` matches any character except a slash `/`.
    59  
    60      l?ss  - matches "less"
    61            - matches "lass"
    62            - doesn't match "floss"
    63  
    64  A `[` and `]` together make a character class, such as `[a-z]` or
    65  `[aeiou]` or `[[:alpha:]]`.  See the [go regexp
    66  docs](https://golang.org/pkg/regexp/syntax/) for more info on these.
    67  
    68      h[ae]llo - matches "hello"
    69               - matches "hallo"
    70               - doesn't match "hullo"
    71  
    72  A `{` and `}` define a choice between elements.  It should contain a
    73  comma separated list of patterns, any of which might match.  These
    74  patterns can contain wildcards.
    75  
    76      {one,two}_potato - matches "one_potato"
    77                       - matches "two_potato"
    78                       - doesn't match "three_potato"
    79                       - doesn't match "_potato"
    80  
    81  Special characters can be escaped with a `\` before them.
    82  
    83      \*.jpg       - matches "*.jpg"
    84      \\.jpg       - matches "\.jpg"
    85      \[one\].jpg  - matches "[one].jpg"
    86  
    87  Patterns are case sensitive unless the `--ignore-case` flag is used.
    88  
    89  Without `--ignore-case` (default)
    90  
    91      potato - matches "potato"
    92             - doesn't match "POTATO"
    93  
    94  With `--ignore-case`
    95  
    96      potato - matches "potato"
    97             - matches "POTATO"
    98  
    99  Note also that rclone filter globs can only be used in one of the
   100  filter command line flags, not in the specification of the remote, so
   101  `rclone copy "remote:dir*.jpg" /path/to/dir` won't work - what is
   102  required is `rclone --include "*.jpg" copy remote:dir /path/to/dir`
   103  
   104  ### Directories ###
   105  
   106  Rclone keeps track of directories that could match any file patterns.
   107  
   108  Eg if you add the include rule
   109  
   110      /a/*.jpg
   111  
   112  Rclone will synthesize the directory include rule
   113  
   114      /a/
   115  
   116  If you put any rules which end in `/` then it will only match
   117  directories.
   118  
   119  Directory matches are **only** used to optimise directory access
   120  patterns - you must still match the files that you want to match.
   121  Directory matches won't optimise anything on bucket based remotes (eg
   122  s3, swift, google compute storage, b2) which don't have a concept of
   123  directory.
   124  
   125  ### Differences between rsync and rclone patterns ###
   126  
   127  Rclone implements bash style `{a,b,c}` glob matching which rsync doesn't.
   128  
   129  Rclone always does a wildcard match so `\` must always escape a `\`.
   130  
   131  ## How the rules are used ##
   132  
   133  Rclone maintains a combined list of include rules and exclude rules.
   134  
   135  Each file is matched in order, starting from the top, against the rule
   136  in the list until it finds a match.  The file is then included or
   137  excluded according to the rule type.
   138  
   139  If the matcher fails to find a match after testing against all the
   140  entries in the list then the path is included.
   141  
   142  For example given the following rules, `+` being include, `-` being
   143  exclude,
   144  
   145      - secret*.jpg
   146      + *.jpg
   147      + *.png
   148      + file2.avi
   149      - *
   150  
   151  This would include
   152  
   153    * `file1.jpg`
   154    * `file3.png`
   155    * `file2.avi`
   156  
   157  This would exclude
   158  
   159    * `secret17.jpg`
   160    * non `*.jpg` and `*.png`
   161  
   162  A similar process is done on directory entries before recursing into
   163  them.  This only works on remotes which have a concept of directory
   164  (Eg local, google drive, onedrive, amazon drive) and not on bucket
   165  based remotes (eg s3, swift, google compute storage, b2).
   166  
   167  ## Adding filtering rules ##
   168  
   169  Filtering rules are added with the following command line flags.
   170  
   171  ### Repeating options ##
   172  
   173  You can repeat the following options to add more than one rule of that
   174  type.
   175  
   176    * `--include`
   177    * `--include-from`
   178    * `--exclude`
   179    * `--exclude-from`
   180    * `--filter`
   181    * `--filter-from`
   182    * `--filter-from-raw`
   183  
   184  **Important** You should not use `--include*` together with `--exclude*`. 
   185  It may produce different results than you expected. In that case try to use: `--filter*`.
   186  
   187  Note that all the options of the same type are processed together in
   188  the order above, regardless of what order they were placed on the
   189  command line.
   190  
   191  So all `--include` options are processed first in the order they
   192  appeared on the command line, then all `--include-from` options etc.
   193  
   194  To mix up the order includes and excludes, the `--filter` flag can be
   195  used.
   196  
   197  ### `--exclude` - Exclude files matching pattern ###
   198  
   199  Add a single exclude rule with `--exclude`.
   200  
   201  This flag can be repeated.  See above for the order the flags are
   202  processed in.
   203  
   204  Eg `--exclude *.bak` to exclude all bak files from the sync.
   205  
   206  ### `--exclude-from` - Read exclude patterns from file ###
   207  
   208  Add exclude rules from a file.
   209  
   210  This flag can be repeated.  See above for the order the flags are
   211  processed in.
   212  
   213  Prepare a file like this `exclude-file.txt`
   214  
   215      # a sample exclude rule file
   216      *.bak
   217      file2.jpg
   218  
   219  Then use as `--exclude-from exclude-file.txt`.  This will sync all
   220  files except those ending in `bak` and `file2.jpg`.
   221  
   222  This is useful if you have a lot of rules.
   223  
   224  ### `--include` - Include files matching pattern ###
   225  
   226  Add a single include rule with `--include`.
   227  
   228  This flag can be repeated.  See above for the order the flags are
   229  processed in.
   230  
   231  Eg `--include *.{png,jpg}` to include all `png` and `jpg` files in the
   232  backup and no others.
   233  
   234  This adds an implicit `--exclude *` at the very end of the filter
   235  list. This means you can mix `--include` and `--include-from` with the
   236  other filters (eg `--exclude`) but you must include all the files you
   237  want in the include statement.  If this doesn't provide enough
   238  flexibility then you must use `--filter-from`.
   239  
   240  ### `--include-from` - Read include patterns from file ###
   241  
   242  Add include rules from a file.
   243  
   244  This flag can be repeated.  See above for the order the flags are
   245  processed in.
   246  
   247  Prepare a file like this `include-file.txt`
   248  
   249      # a sample include rule file
   250      *.jpg
   251      *.png
   252      file2.avi
   253  
   254  Then use as `--include-from include-file.txt`.  This will sync all
   255  `jpg`, `png` files and `file2.avi`.
   256  
   257  This is useful if you have a lot of rules.
   258  
   259  This adds an implicit `--exclude *` at the very end of the filter
   260  list. This means you can mix `--include` and `--include-from` with the
   261  other filters (eg `--exclude`) but you must include all the files you
   262  want in the include statement.  If this doesn't provide enough
   263  flexibility then you must use `--filter-from`.
   264  
   265  ### `--filter` - Add a file-filtering rule ###
   266  
   267  This can be used to add a single include or exclude rule.  Include
   268  rules start with `+ ` and exclude rules start with `- `.  A special
   269  rule called `!` can be used to clear the existing rules.
   270  
   271  This flag can be repeated.  See above for the order the flags are
   272  processed in.
   273  
   274  Eg `--filter "- *.bak"` to exclude all bak files from the sync.
   275  
   276  ### `--filter-from` - Read filtering patterns from a file ###
   277  
   278  Add include/exclude rules from a file.
   279  
   280  This flag can be repeated.  See above for the order the flags are
   281  processed in.
   282  
   283  Prepare a file like this `filter-file.txt`
   284  
   285      # a sample filter rule file
   286      - secret*.jpg
   287      + *.jpg
   288      + *.png
   289      + file2.avi
   290      - /dir/Trash/**
   291      + /dir/**
   292      # exclude everything else
   293      - *
   294  
   295  Then use as `--filter-from filter-file.txt`.  The rules are processed
   296  in the order that they are defined.
   297  
   298  This example will include all `jpg` and `png` files, exclude any files
   299  matching `secret*.jpg` and include `file2.avi`.  It will also include
   300  everything in the directory `dir` at the root of the sync, except
   301  `dir/Trash` which it will exclude.  Everything else will be excluded
   302  from the sync.
   303  
   304  ### `--files-from` - Read list of source-file names ###
   305  
   306  This reads a list of file names from the file passed in and **only**
   307  these files are transferred.  The **filtering rules are ignored**
   308  completely if you use this option.
   309  
   310  `--files-from` expects a list of files as its input. Leading / trailing
   311  whitespace is stripped from the input lines and lines starting with `#`
   312  and `;` are ignored.
   313  
   314  Rclone will traverse the file system if you use `--files-from`,
   315  effectively using the files in `--files-from` as a set of filters.
   316  Rclone will not error if any of the files are missing.
   317  
   318  If you use `--no-traverse` as well as `--files-from` then rclone will
   319  not traverse the destination file system, it will find each file
   320  individually using approximately 1 API call. This can be more
   321  efficient for small lists of files.
   322  
   323  This option can be repeated to read from more than one file.  These
   324  are read in the order that they are placed on the command line.
   325  
   326  Paths within the `--files-from` file will be interpreted as starting
   327  with the root specified in the command.  Leading `/` characters are
   328  ignored. See [--files-from-raw](#files-from-raw-read-list-of-source-file-names-without-any-processing)
   329  if you need the input to be processed in a raw manner.
   330  
   331  For example, suppose you had `files-from.txt` with this content:
   332  
   333      # comment
   334      file1.jpg
   335      subdir/file2.jpg
   336  
   337  You could then use it like this:
   338  
   339      rclone copy --files-from files-from.txt /home/me/pics remote:pics
   340  
   341  This will transfer these files only (if they exist)
   342  
   343      /home/me/pics/file1.jpg        → remote:pics/file1.jpg
   344      /home/me/pics/subdir/file2.jpg → remote:pics/subdir/file2.jpg
   345  
   346  To take a more complicated example, let's say you had a few files you
   347  want to back up regularly with these absolute paths:
   348  
   349      /home/user1/important
   350      /home/user1/dir/file
   351      /home/user2/stuff
   352  
   353  To copy these you'd find a common subdirectory - in this case `/home`
   354  and put the remaining files in `files-from.txt` with or without
   355  leading `/`, eg
   356  
   357      user1/important
   358      user1/dir/file
   359      user2/stuff
   360  
   361  You could then copy these to a remote like this
   362  
   363      rclone copy --files-from files-from.txt /home remote:backup
   364  
   365  The 3 files will arrive in `remote:backup` with the paths as in the
   366  `files-from.txt` like this:
   367  
   368      /home/user1/important → remote:backup/user1/important
   369      /home/user1/dir/file  → remote:backup/user1/dir/file
   370      /home/user2/stuff     → remote:backup/user2/stuff
   371  
   372  You could of course choose `/` as the root too in which case your
   373  `files-from.txt` might look like this.
   374  
   375      /home/user1/important
   376      /home/user1/dir/file
   377      /home/user2/stuff
   378  
   379  And you would transfer it like this
   380  
   381      rclone copy --files-from files-from.txt / remote:backup
   382  
   383  In this case there will be an extra `home` directory on the remote:
   384  
   385      /home/user1/important → remote:backup/home/user1/important
   386      /home/user1/dir/file  → remote:backup/home/user1/dir/file
   387      /home/user2/stuff     → remote:backup/home/user2/stuff
   388  
   389  ### `--files-from-raw` - Read list of source-file names without any processing ###
   390  This option is same as `--files-from` with the only difference being that the input
   391  is read in a raw manner. This means that lines with leading/trailing whitespace and
   392  lines starting with `;` or `#` are read without any processing. [rclone lsf](/commands/rclone_lsf/)
   393  has a compatible format that can be used to export file lists from remotes, which
   394  can then be used as an input to `--files-from-raw`.
   395  
   396  ### `--min-size` - Don't transfer any file smaller than this ###
   397  
   398  This option controls the minimum size file which will be transferred.
   399  This defaults to `kBytes` but a suffix of `k`, `M`, or `G` can be
   400  used.
   401  
   402  For example `--min-size 50k` means no files smaller than 50kByte will be
   403  transferred.
   404  
   405  ### `--max-size` - Don't transfer any file larger than this ###
   406  
   407  This option controls the maximum size file which will be transferred.
   408  This defaults to `kBytes` but a suffix of `k`, `M`, or `G` can be
   409  used.
   410  
   411  For example `--max-size 1G` means no files larger than 1GByte will be
   412  transferred.
   413  
   414  ### `--max-age` - Don't transfer any file older than this ###
   415  
   416  This option controls the maximum age of files to transfer.  Give in
   417  seconds or with a suffix of:
   418  
   419    * `ms` - Milliseconds
   420    * `s` - Seconds
   421    * `m` - Minutes
   422    * `h` - Hours
   423    * `d` - Days
   424    * `w` - Weeks
   425    * `M` - Months
   426    * `y` - Years
   427  
   428  For example `--max-age 2d` means no files older than 2 days will be
   429  transferred.
   430  
   431  This can also be an absolute time in one of these formats
   432  
   433  - RFC3339 - eg "2006-01-02T15:04:05Z07:00"
   434  - ISO8601 Date and time, local timezone - "2006-01-02T15:04:05"
   435  - ISO8601 Date and time, local timezone - "2006-01-02 15:04:05"
   436  - ISO8601 Date - "2006-01-02" (YYYY-MM-DD)
   437  
   438  ### `--min-age` - Don't transfer any file younger than this ###
   439  
   440  This option controls the minimum age of files to transfer.  Give in
   441  seconds or with a suffix (see `--max-age` for list of suffixes)
   442  
   443  For example `--min-age 2d` means no files younger than 2 days will be
   444  transferred.
   445  
   446  ### `--delete-excluded` - Delete files on dest excluded from sync ###
   447  
   448  **Important** this flag is dangerous - use with `--dry-run` and `-v` first.
   449  
   450  When doing `rclone sync` this will delete any files which are excluded
   451  from the sync on the destination.
   452  
   453  If for example you did a sync from `A` to `B` without the `--min-size 50k` flag
   454  
   455      rclone sync A: B:
   456  
   457  Then you repeated it like this with the `--delete-excluded`
   458  
   459      rclone --min-size 50k --delete-excluded sync A: B:
   460  
   461  This would delete all files on `B` which are less than 50 kBytes as
   462  these are now excluded from the sync.
   463  
   464  Always test first with `--dry-run` and `-v` before using this flag.
   465  
   466  ### `--dump filters` - dump the filters to the output ###
   467  
   468  This dumps the defined filters to the output as regular expressions.
   469  
   470  Useful for debugging.
   471  
   472  ### `--ignore-case` - make searches case insensitive ###
   473  
   474  Normally filter patterns are case sensitive.  If this flag is supplied
   475  then filter patterns become case insensitive.
   476  
   477  Normally a `--include "file.txt"` will not match a file called
   478  `FILE.txt`.  However if you use the `--ignore-case` flag then
   479  `--include "file.txt"` this will match a file called `FILE.txt`.
   480  
   481  ## Quoting shell metacharacters ##
   482  
   483  The examples above may not work verbatim in your shell as they have
   484  shell metacharacters in them (eg `*`), and may require quoting.
   485  
   486  Eg linux, OSX
   487  
   488    * `--include \*.jpg`
   489    * `--include '*.jpg'`
   490    * `--include='*.jpg'`
   491  
   492  In Windows the expansion is done by the command not the shell so this
   493  should work fine
   494  
   495    * `--include *.jpg`
   496  
   497  ## Exclude directory based on a file ##
   498  
   499  It is possible to exclude a directory based on a file, which is
   500  present in this directory. Filename should be specified using the
   501  `--exclude-if-present` flag. This flag has a priority over the other
   502  filtering flags.
   503  
   504  Imagine, you have the following directory structure:
   505  
   506      dir1/file1
   507      dir1/dir2/file2
   508      dir1/dir2/dir3/file3
   509      dir1/dir2/dir3/.ignore
   510  
   511  You can exclude `dir3` from sync by running the following command:
   512  
   513      rclone sync --exclude-if-present .ignore dir1 remote:backup
   514  
   515  Currently only one filename is supported, i.e. `--exclude-if-present`
   516  should not be used multiple times.