1

I want to match all files with a typical fortran file extension with find. Typical extensions are .f .F .for .FOR .f90 .F90 .f95 .F95 .f03 .F03 .f08 .F08 .f15 .F15 .f18 .F18

what I tried so far is find * -name "*\.[Ff][0-9Oo][0-9Rr]" this works for all the mentioned extensions despite .f and .F. For that I have to make [0-9Oo] and [0-9Rr] optional which is usually done with "?". However, this find * -name "*\.[Ff][0-9Oo]?[0-9Rr]?" does not work with my version of find.

M0M0
  • 200
  • 9

1 Answers1

1

With a GNU find, you can use

find * -regextype posix-extended -regex '.*\.[Ff][0-9Oo]?[0-9Rr]?'

The pattern matches a string that fully matches:

  • .*\. - any text up to the rightmost . including it
  • [Ff] - F or f
  • [0-9Oo]? - an optional digit, O or o
  • [0-9Rr]? - an optional digit, R or r.

You can also use a non-regex approach using mere glob patterns:

find * ( -name "*.[Ff][0-9oO][0-9rR]" -o -name "*.[Ff]" )

Note that here, the * glob pattern matches any text. In the previous solution, it was a quantifier, an operator that makes the preceding pattern match zero or more times. Also, in glob patterns, . matches a literal dot, no need to escape it.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563