Filter out files matching a pattern in bash

Viewed 33

I have the following list of files in bash

[ foo_file1.yaml, file2.yaml, anotherfile.yaml ]

I want to filter out files matching the pattern foo*.yaml

The following test does not seem to work

          for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
            if [[ $file == foo*.yaml ]]; then
              echo $file
            fi

(it is from a step in a GHA job but I don't think this is relevant)

Any suggestions?

edit: I have also tried this

echo $file | grep -e 'foo*\.yaml'

still doesn't work.

1 Answers

If you are using grep, you don't need to actually check the condition. Something like this should work:

#!/bin/bash

for file in "foo_file1.yaml" "file2.yaml" "anotherfile.yaml"; do
 echo $file | grep -o 'foo.*.yaml'
done

It outputs:

foo_file1.yaml
Related