This kind of file system interaction is a bit hard. You'd think that someone would have created a module for that, but there are precious few, and I haven't come across one that was easy to use.
What most people do is either use a specific call to the system
or execute_command_line
subroutines to let the shell do the work, see here:
program my_ls
use iso_fortran_env
implicit none
character(len=*), parameter :: ls_file = '/tmp/my_ls.tmp'
integer :: u, ios
character(len=30) :: filename
call execute_command_line('ls -1 > '//ls_file, wait=.TRUE., exitstat=ios)
if (ios /= 0) stop "Unable to get listing"
open(newunit=u, file=ls_file, iostat=ios, status="old", action="read")
if ( ios /= 0 ) stop "Error opening listing file "
do
read(u, *, iostat=ios) filename
if (is_iostat_end(ios)) exit
if (ios /= 0) STOP "Unexpected error while reading listing file"
if (index(filename, ".cel") > 0) then
print*, filename
end if
end do
close(u)
call execute_command_line('rm '//ls_file, wait=.FALSE.)
end program my_ls
But what would be easier is to give the filenames as a command line argument when starting the program:
program my_ls2
use iso_fortran_env
implicit none
integer :: num_files, i
character(len=64), allocatable :: filenames(:)
num_files = command_argument_count()
allocate(filenames(num_files))
do i = 1, num_files
call get_command_argument(i, filenames(i))
end do
write(*, '(A)') filenames
end program my_ls2
Calling this program with
$ my_ls2 *.cel
will give you every file you want.