Bash 检查目录中是否有1个或多个特定文件?
我需要创建如下脚本:Bash 检查目录中是否有1个或多个特定文件?,bash,Bash,我需要创建如下脚本: if directory contains atleast one file (1 or more) that starts with 'naz' #do something else (contains 0 files that start with 'naz') #do something else 我的问题是当我尝试 if [ -f naz* ] 我最终得到了 bash:[:参数太多 因为满足条件的文件超过1个。我正在寻找一种可以处理找到的多个文件
if directory contains atleast one file (1 or more) that starts with 'naz'
#do something
else (contains 0 files that start with 'naz')
#do something else
我的问题是当我尝试
if [ -f naz* ]
我最终得到了
bash:[:参数太多
因为满足条件的文件超过1个。我正在寻找一种可以处理找到的多个文件的解决方案。您不能对多个文件使用这样的测试 最好是:
shopt -s nullglob
compgen -W naz* &>/dev/null
case $? in
0) echo "only one file match" ;;
1) echo "more than one file match" ;;
2) echo "no file match" ;;
esac
此解决方案可能需要一段时间才能在包含大量文件的目录中进行计算,但更简洁
if[`find.-name“naz*”-类型f-d1 | wc-l`-ge 1]
如果要转到子目录,可以删除-d 1
部分