Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for digits in Unix find command

I have this command:

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

And I need to beef it up so the part before the file extensions matches a YYYY/MM/DD pattern, like so:

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'm failing to get any permutation of \d and parens escaping to work.

UPDATE: here's what worked for me based on the accepted 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\)"
like image 501
Teflon Ted Avatar asked Jan 10 '11 21:01

Teflon Ted


2 Answers

\d is an extension of regular expressions that is not supported by Emacs regular expressions and POSIX regular expressions (those are the flavours find supports). You can use [[:digit:]] or [0-9] instead.

like image 132
Jan Krüger Avatar answered Oct 06 '22 00:10

Jan Krüger


You can use the repeaters like this:

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

I use this to find backups of the form:

./foo._20140716-121745.OLD

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

(on CentOS 6.5)

P.S. -regextype posix-extended works too.

like image 23
Bloke Down The Pub Avatar answered Oct 05 '22 22:10

Bloke Down The Pub