Issue
How can I use the test
command for an arbitrary number of files, passed in using an argument with a wildcard?
For example:
test -f /var/log/apache2/access.log.* && echo "exists one or more files"
Currently, it prints
error: bash: test: too many arguments
Solution
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
By the way, /var/log/apache2/access.log.*
is called shell-globbing, not regexp. Please see Confusion with shell-globbing wildcards and Regex for more information.
Answered By - Hui Zheng Answer Checked By - Dawn Plyler (WPSolving Volunteer)