linux - 仅通过文件名检查文件是否存在(不带扩展名)

标签 linux bash shell

这很好用

if [[ -e img.png ]]
then
     echo "exist"
else
     echo "doesn't exist"
fi

但是,如果我知道可能有名称为 img 的图片,但我不知道该文件是 .jpg 、 .gif 、 .jpeg 、 .tff 等等,该怎么办。

我不在乎扩展名是什么我只想知道是否有名为“img”的文件

我该怎么做?

最佳答案

你可以这样做:

files=$(ls img.* 2> /dev/null | wc -l)
if [ "$files" != "0" ]
then
echo "exist"
else
echo "doesn't exist"
fi

关于linux - 仅通过文件名检查文件是否存在(不带扩展名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13247069/

相关文章:

javascript - 执行 exe nodejs,完成时标记

c - 如何仅从依赖库中删除符号?

bash - -bash : vim: command not found

linux - 在 Linux shell 中的文件夹中压缩文件夹

linux - 是否可以在子 shell 中修改父变量?

python - 如何使用正则表达式来匹配文件中其他小写单词不连续重复的大写单词

linux - 读取并删除名称中包含空格的文件

linux - 在 crontab 中调度脚本时错误替换错误

linux - 可以看到Wordpress登录访问日志吗?

php - mysql导出后找不到文件