0

I would like to list all Fortran 90 files in bash without files ending in _tst.f90. My best try so far is

ls -l ../src/*[!_tst].f90

According to Bash reference on pattern matching [!_tst] takes into account only single occurences of _, t and s. Is it possible to tell bash that _tst should be a sequence? Brackets (_tst), single '_tst' and double quote "_tst" symbols do not work.

1
  • You want find files ending how, exactly? And what do you want NOT to find? Commented Oct 25, 2013 at 10:22

3 Answers 3

0

Strictly answering your question: yes you can, but it doesn't work.

shopt -s extglob
ls -l ../src/*!(_tst).f90

The problem is that !(...) will happily match an empty string, so any filename foo_tst.f90 will just have the * swallow up the first seven characters.

I don't know a bash-native way to do this without using find(1).

In zsh:

setopt extended_glob
ls -ld ../src/*.f90~*_tst.f90

This uses the ~ extended_glob feature, where A~B results in anything matching A that does not match B.

0

If you want to find all files ending in f90, except those ending in _tst.f90, you should use the command:

find /spath/to/start/directory -name '*.f90' ! -name '*_tst.f90' -print

Edit: This goes down the whole tree. If you want to limit yourself to the current directory,

find /spath/to/start/directory -name '*.f90' ! -name '*_tst.f90' -maxdepth 1 -print
0

This is an expansion on the shopt -s extglob path:

shopt -s extglob
ls -l ../src/!(!(*.f90)|*_tst.f90)

which tells Bash to match anything that does not match either of:

  • not *.f90, i.e. anything but .f90 files or

  • _tst.f90, i.e. your test files.

which match only the *.f90 files except *_tst.f90.

It quickly becomes convoluted, and find is in my mind a more extensible solution for complex file matching, but if you want Bash only, similar constructs are possible.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .