Regex for numbers in a Unix search command

I have this command:

find reports/ -type f -mtime +90 -regex ".*\.\(csv\|sql\|txt\|xls\|zip\)" 

And I need to strengthen it so that the part before the file extension matches the YYYY/MM/DD pattern, for example:

 reports/2010/10/10/23.txt reports/2010/10/10/23.xls reports/2010/10/10/26.csv reports/2010/10/10/26.sql reports/2010/10/10/26.txt reports/2010/10/10/26.xls reports/2010/10/10/27.csv 

But I can't get any permutation \d and parens triggered to work.

UPDATE: this is what worked for me based on the answer below:

 find reports/ -type f -mtime +90 -regex "reports/201[01]/\([1-9]\|1[012]\)/\([1-9]\|[12][0-9]\|3[01]\)/.*\.\(csv\|sql\|txt\|xls\|zip\)" 
+7
source share
4 answers

This is what I used in the past:

 Year: (19|20)[0-9][0-9] Month: 0[1-9]|1[012] Day: (0[1-9]|[12][0-9]|3[01]) 

You can put them in your regular expression. You should of course avoid brackets and pipes.

+5
source

\d is a regular expression extension that is not supported by Emacs regular expressions and POSIX regular expressions (this is find support). Instead, you can use [[:digit:]] or [0-9] .

+3
source

The following is ugly and does not negate invalid dates, but may be close enough:

 find reports/ -type f -regex ".*/reports/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[0-9][0-9]\.\(csv\|sql\|txt\|xls\|zip\)" 
+3
source

You can use repeaters as follows:

 find ./ -regextype posix-egrep -iregex ".*\._[0-9]{8}-[0-9]{6}.*" 

I use this to find form backups:

 ./foo._20140716-121745.OLD 

Where foo is the original name and numbers are the date and time.

(on CentOS 6.5)

PS -regextype posix-extended also works.

0
source

All Articles