2

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,066
  • 8
  • 36
  • 46

3 Answers3

3

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
  • 307,646
  • 55
  • 250
  • 337
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
  • 71,634
  • 16
  • 123
  • 171
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,165
  • 3
  • 39
  • 64