Stupidly, I had been using a condition like this as part of a script:
if [ $(ls FOO* 2> /dev/null) ] # if files named "FOO*" were downloaded
then
echo "Files found"
# ... process and email results
else
echo "Not found"
# ... email warning that no files were found (against expectations)
fi
That works for zero and one files named FOO*
, but fails if there are more than one. From logs I found several different error messages stemming from this:
[: FOO_20131107_082920: unary operator expected
[: FOO_20131108_070203: binary operator expected
[: too many arguments
My question is: what is the correct way to check, in a Bash if
condition, whether one or more files whose name begins with FOO
exist?
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
find
as explained here on stackoverflow. – Joshua Goldberg Nov 18 '19 at 17:48