2

How do I test if a file (or many) named from given pattern exists and do stuff based on that test?

This is my failed attempt:

[[ { ls ../outputListWorkerPid_* |  wc -l } -ge "1" ]] && echo "ARRRRRR" || && echo "FAIL"

But I got the following error:

bash: conditional binary operator expected
bash: syntax error near `ls'

Also it would be nice to avoid the No such file or directory from ls in case the doesn't exists.

RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
  • 1
    Does this answer help you at all? http://stackoverflow.com/questions/6363441/check-if-a-file-exists-with-wildcard-in-shell-script – AlexLordThorsen Apr 09 '13 at 17:01

1 Answers1

-1

Use an if-statement:

if [ "$(ls *.txt | wc -l)" -ge "1" ];
then
  echo "Yes";
else
  echo "No";
fi;

Does that answer your question?

Matt W
  • 476
  • 3
  • 11
  • 2
    If there are no *.txt files I get "ls: cannot access *.txt: No such file or directory" – Phil Dec 29 '20 at 22:43