在 Bash 中的文件夹内的所有文件名中出现一个字符串

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

我正在尝试制作一个脚本,它允许我 select 名称中出现 2 次或更多次字符串的文件。

示例:

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

我希望脚本 return 只有文件 2 和 3。我这样尝试过,但这没有用:

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

您可以使用:

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

-wc是一个字数

在 shell 脚本中如果 $result>1 做一些事情...

grepwc 太过分了。一个简单的 glob 就足够了:

*test*test*

你可以这样使用:

ls *test*test*

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