我正在尝试制作一个脚本,该脚本允许我选择名称中包含2次或更多次出现的字符串的文件。
例:
test.txt // 1 occurrence only, not ok
test-test.txt // 2 occurrences OK
test.test.txt // 2 occurrences OK
我希望脚本仅向我返回文件2和3。我尝试这样做,但这没有用:
rows=$(ls | grep "test")
for file in $rows
do
if [[ $(wc -w $file) == 2 ]]; then
echo "the file $file matches"
fi
done
grep
并且wc
是过度杀伤力。一个简单的glob就足够了:
*test*test*
您可以这样使用:
ls *test*test*
要么
for file in *test*test*; do
echo "$file"
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句