4

I am trying to find all the files whose name contains exactly 14 digits (I'm trying to match a timestamp in the filename). I'm not sure how to get the GNU find regexp syntax for repetitions right.

I've tried find -regex ".*[0-9]{14} and find -regex ".*[0-9]\{14\}, neither of these turns up any results. Can you help me with the syntax?

benhsu
  • 5,346
  • 8
  • 39
  • 47

4 Answers4

2

Try changing the -regextype parameter to find.

Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-extended.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
2

remember, GNU find's -regex matches a whole path. Anyway, you can use a combination of find and grep to do the task, eg to find exactly 14 digits with no other characters

find . -type f -printf "%f\n" | grep -E "\b[0-9]{14}\b"

modify to suit your needs

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

Strange, I just gave it a try and I could not get this work. Here's a workaround anyway (matching 2 consecutive numbers):

$ls
a123.txt  a1b2c3.txt  a45.txt  b123.txt
$find -regex '.*[^0-9][0-9][0-9][^0-9].*'
./a45.txt
bpgergo
  • 15,669
  • 5
  • 44
  • 68
0
  1. -regextype should match your -regex or -iregex
  2. the -regex and -iregex must occur later on the command line.
  3. -regex matches whole path

Example:

$ find . -regextype grep -regex '.*/[0-9]\{14\}.*'                                                                                                                                                                                                                                  
./00000000000054                                                                                                                                                                                                                                                                          
./00000000000098                                                                                                                                                                                                                                                                          
./00000000000042                                                                                                                                                                                                                                                                          
./00000000000075                                                                                                                                                                                                                                                                          
./00000000000027                                                                                                                                                                                                                                                                          
./00000000000080                                                                                                                                                                                                                                                                          
./00000000000083                                                                                                                                                                                                                                                                          
./00000000000077
Qiu Yangfan
  • 871
  • 11
  • 25