Occurring string in all filenames in a folder in Bash

I am trying to make a script that allows me to select files with 2 or more occurrences of a line 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 only return files 2 and 3 to me. I tried this way but it 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

      

+3


source to share


2 answers


grep

and wc

are redundant. A simple ball is enough:

*test*test*

      

You can use it like this:



ls *test*test*

      

or

for file in *test*test*; do
    echo "$file"
done

      

+3


source


You can use:

result=$(grep -o "test" yourfile | wc -l)



-wc - word count

In a shell script if $result>1

doing stuff ...

0


source







All Articles