How to filter filenames start with certain partten?

What is the problem you are having with rclone?

I am trying to filter files using regex. I have read this regexp section at Rclone Filtering

But I still cannot figure out, any guide would be appreciate, thanks! :smiley:

Run the command 'rclone version' and share the full output of the command.

āžœ rclone version
rclone v1.65.0
- os/version: darwin 14.6.1 (64 bit)
- os/kernel: 23.6.0 (arm64)
- os/type: darwin
- os/arch: arm64 (ARMv8 compatible)
- go/version: go1.21.4
- go/linking: dynamic
- go/tags: none

Which cloud storage system are you using? (eg Google Drive)

N/A

The command you were trying to run (eg rclone copy /tmp remote:tmp)

I am trying to use --filter to only include files

  • starting with T-
  • ending with .txt

I have files at /tmp/a/ folder:

T-T-aa.txt
T-a.txt
T-aa.txt
T.txt
a-T-aa.txt

So I hope to only return

T-T-aa.txt
T-a.txt
T-aa.txt

However, right now, it always returns all files

āžœ rclone ls /tmp/a/ --filter="+ {{^T-[^/]*\.txt}}"
        0 T-T-aa.txt
        0 T-a.txt
        0 T-aa.txt
        0 T.txt
        0 a-T-aa.txt

Please run 'rclone config redacted' and share the full output. If you get command not found, please make sure to update rclone.

N/A

A log from the command that you were trying to run with the -vv flag

āžœ rclone ls /tmp/a/ --filter="+ {{^T-[^/]*\.txt}}" -vv
2024/09/16 13:25:18 INFO  : Can't figure out directory filters from "{{^T-[^/]*\\.txt}}": looking in all directories
2024/09/16 13:25:18 DEBUG : rclone: Version "v1.65.0" starting with parameters ["rclone" "ls" "/tmp/a/" "--filter=+ {{^T-[^/]*\\.txt}}" "-vv"]
2024/09/16 13:25:18 DEBUG : Creating backend with remote "/tmp/a/"
2024/09/16 13:25:18 DEBUG : Using config file from "/Users/hongbo-miao/.config/rclone/rclone.conf"
2024/09/16 13:25:18 DEBUG : fs cache: renaming cache item "/tmp/a/" to be canonical "/tmp/a"
        0 T-T-aa.txt
        0 T-a.txt
        0 T-aa.txt
        0 T.txt
        0 a-T-aa.txt
2024/09/16 13:25:18 DEBUG : 5 go routines active

Which is correct as I think you missed this key point from docs:

--filter + differs from --include. In the case of --include rclone implies an --exclude * rule which it adds to the bottom of the internal rule list. --filter...+ does not imply that rule.

So you need at least two filters:

+ your filter
- *

Also:

Feel free to practice regular expressions (I recommend https://regex101.com/ as a playground) but for such trivial requirement simple wildcard include rule should suffice:

rclone ls /path --include "T-*.txt"

or if you insist on using filters:

rclone ls /path --filter "+ T-*.txt" --filter "- *"

For regex in rclone pay attention to its quirks and use --dump filters flag to debug:

$ rclone ls . --filter "+ {{T-.*\.txt}}" --filter "- *" --dump filters
2024/09/16 07:27:50 NOTICE: Automatically setting -vv as --dump is enabled
2024/09/16 07:27:50 INFO  : Can't figure out directory filters from "{{T-.*\\.txt}}": looking in all directories
--- start filters ---
--- File filter rules ---
+ (^|/)(T-.*\.txt)$
- (^|/)[^/]*$
--- Directory filter rules ---
+ ^.*$
- ^.*$
--- end filters ---
2024/09/16 07:27:50 DEBUG : rclone: Version "v1.68.0" starting with parameters ["rclone" "ls" "." "--filter" "+ {{T-.*\\.txt}}" "--filter" "- *" "--dump" "filters"]
2024/09/16 07:27:50 DEBUG : Creating backend with remote "."
2024/09/16 07:27:50 DEBUG : Using config file from "/Users/kptsky/.config/rclone/rclone.conf"
2024/09/16 07:27:50 DEBUG : fs cache: renaming cache item "." to be canonical "/Users/kptsky/Temp/test/filter"
        0 T-T-aa.txt
        0 T-a.txt
        0 T-aa.txt
2024/09/16 07:27:50 DEBUG : 5 go routines active
1 Like

Thank you so much for the detailed explanation!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.