Occurrence of a string in all the file names within a folder in Bash

Viewed 92

I am trying to make a script which allow me to select files with 2 or more occurrences of a string in their name.

Example:

test.txt      // 1 occurrence only, not ok
test-test.txt // 2 occurrences OK
test.test.txt // 2 occurrences OK

I want the script to return me only the files 2 and 3. I tried like that but this didn't work:

rows=$(ls | grep "test")
for file in $rows
do
        if [[ $(wc -w $file) == 2 ]]; then
                echo "the file $file matches"
        fi
done
2 Answers
Related