Stephen Kwan
Stephen Kwan

Reputation: 21

Bash command to find all Fortran files in a directory

I am trying to find all the Fortran files in a directory and replace some text in that file.

At first i was thinking of using find -regex ... -exec to find all the file extensions for fortran code and make replacements. However, there are a lot of file extensions, is there another way to identify fortran files?

Upvotes: 0

Views: 665

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18118

Here is a very short find command to find all Fortran related files. The most common Fortran files are *.f, *.f90, and their capital letter counterparts. Additionally, .f95, .f03, .f08, and even .for are used. Note that -iregex matches the regular expression case insensitive (in contrast to -regex).

find . -iregex ".*\.F[0-9]*" -o -iregex ".*\.for" 

To do something with this, you can use xargs:

find . -iregex ".*\.F[0-9]*" -o -iregex ".*\.for" | xargs head -1

Replace head -1 with whatever you want to do with the files.


Another way to work with that is using a loop such as:

for file in $(find . -iregex ".*\.F[0-9]*" -o -iregex ".*\.for"); do
  head -1 "$file"
done

This gives you a bit more flexibility than the xargs approach, and is easier to read.

Upvotes: 2

Related Questions