How can i use test command for arbitrary number of files, passed in argument by regexp
for example:
test -f /var/log/apache2/access.log.* && echo "exists one or more files"
but mow print error: bash: test: too many arguments
This solution seems to me more intuitive:
if [ `ls -1 /var/log/apache2/access.log.* 2>/dev/null | wc -l ` -gt 0 ]; then echo "ok" else echo "ko" fi
To avoid "too many arguments error", you need xargs. Unfortunately, test -f
doesn't support multiple files. The following one-liner should work:
for i in /var/log/apache2/access.log.*; do test -f "$i" && echo "exists one or more files" && break; done
BTW, /var/log/apache2/access.log.*
is called shell-globbing, not regexp, please check this: Confusion with shell-globbing wildcards and Regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With