find regex with date range in filename

1.6k Views Asked by At

I've files in a folder like

file_2017-01-01.jpg    
file_2017-02-20.jpg    
file_2017-05-10.jpg    
file_2017-09-01-jpg    
file_2017-10-25.jpg    
file_2017-11-04.jpg    
file_2017-12-22.jpg

How can I use the find command to list files from file_2017-01* to file_2017-10* ?

find . -regextype posix-awk -regex ".*2017-[01-10]*" doesn't work.

Also not, find . -maxdepth 1 -type f -name "*2017-[01-10]*"

1

There are 1 best solutions below

1
On

A character class only matches one character at a time, so on its own, it's not an adequate tool for the job in question. Consider instead:

find  . -regextype posix-awk -regex '.*_2017-(0[1-9]|10)-.*' -print

This has two branches: 0[1-9], matching any two-character string from 01 through 09; and 10, matching only the exact string 10.