github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/docs/content/filtering.md (about)

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