6

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?

2
  • What are you matching them against? Commented Aug 30, 2011 at 14:59
  • 3
    The top answer here by Susam Pal worked for me: stackoverflow.com/questions/6844785/… Commented Jun 9, 2012 at 7:18

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1
  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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.