0

File I want to find.

20200521-285353-rpi3-4497-00-0-13-0-0-0-0-n.jpg
Files with Date in the filename from 2020-01-01 to 2020-09-31

not

20201021-*.*

My regex string:

^20200[0-9][0-3][0-9]-[0-2].*

Find version: find (GNU findutils) 4.7.0-git

I can't find a combination that works. My attempts:

find . -type f -regextype grep -regex '20200[1-9]-*.*'
find . -type f -regex '^20200[0-9][0-9][0-9]\-[0-2].*'
find . -type f -regextype sed -regex '^20200[0-9][0-9][0-9]\-[0-2].*'
4
  • 1
    Probably the most important thing to note is that -regex is a whole path match. So for example your second attempt would match if you replaced ^ at the start with .*/ (although the escaping of - is not necessary). This behavior is different from that of -name which (as well as being a glob match rather than a regex) matches against the file name only. Oct 24, 2020 at 19:01
  • Please edit your question and clarify what should be found and what should not be found. Should 20200521.txt be found? How about 2020071128847297923497432-4234y9234-foo-bar? We need to know the exact parameters to help you.
    – terdon
    Oct 24, 2020 at 19:16
  • The escaping of " - " is Not from me, a User put it in my String.
    – ThomasDr
    Oct 24, 2020 at 19:53
  • ThomasDr the \- was in your original version. If you feel your question has been edited wrongly, please do fix it
    – roaima
    Oct 24, 2020 at 20:24

2 Answers 2

3

Use a glob and keep it simple

find -type f -name '20200521-*'
2

If  you  want  to  find  only  20200521-285353-rpi3-4497-00-0-13-0-0-0-0-n.jpg, use

find . -type f -name 20200521-285353-rpi3-4497-00-0-13-0-0-0-0-n.jpg

If you want to find files whose names look like

YYYYMMDD-
followed by
         0, 1 or 2
followed by
                  (something)
where YYYY is 2020, use

find . -type f -name '2020[01][0-9][0-3][0-9]-[0-2]*'

but be aware that it will match false positives like 0231 (February 31), 0400 (April 0), 1038 (October 38) and 1701 (the first day of the 17th month).  If you want to find files whose names look like the above, but only for January (01) through September (09), excluding October, November and December (10, 11 and 12), use

find . -type f -name '20200[0-9][0-3][0-9]-[0-2]*'

Note that the above all end with * and not.*.

Please edit your question to clarify what you want to find.

And by the way, what is 285353?  It isn’t a valid HHMMSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.