bash - 如何使用 Bash 脚本测试是否存在多个文件

标签 bash

如何对任意数量的文件使用 test 命令,使用带通配符的参数传入?

例如:

test -f /var/log/apache2/access.log.* && echo "exists one or more files"

目前,它打印

error: bash: test: too many arguments

最佳答案

这个解决方案在我看来更直观:

if [ `ls -1 /var/log/apache2/access.log.* 2>/dev/null | wc -l ` -gt 0 ];
then
    echo "ok"
else
    echo "ko"
fi

关于bash - 如何使用 Bash 脚本测试是否存在多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14765569/

相关文章:

laravel - 如何将 ~/.composer/vendor/bin 目录放在你的 PATH 中?

linux - Bash 脚本和别名

linux - 在 bash 脚本中打开 pdf 文件的命令

bash - 在 getopts 之后使用 shift $((OPTIND-1)) 的原因是什么?

linux - bash 按 yyyy-mm-dd 在 N 天内 bash 中 2 个日期之间的差异

bash - 将 mysql 转储文件重命名为当前日期,然后对其进行 gzip

python - 从 python 脚本更改目录 : how to not open a new shell

bash - 在波浪号之前扩展变量

sql - 使用 SQL Server 在 Linux 上运行目录中的所有 .sql 文件

linux - 在 Bash shell if 语句中使用逻辑 OR