我想到的三种方法:
1)对路径中的名称进行简短测试(我不确定这可能是您的情况)
ls -a "path" | grep "name"
2)对文件中的字符串进行简短测试
grep -R "string" "filepath"
3)使用正则表达式的更长的bash脚本:
#!/bin/bash
declare file="content.txt"
declare regex="\s+string\s+"
declare file_content=$( cat "${file}" )
if [[ " $file_content " =~ $regex ]] # please note the space before and after the file content
then
echo "found"
else
echo "not found"
fi
exit
如果您必须使用循环来测试文件内容上的多个字符串(例如在任何cicle上更改正则表达式),则此方法应该更快。