sh - Check if a file exists with wildcard in shell script -
i'm trying check if file exists, wildcard. here example:
if [ -f "xorg-x11-fonts*" ]; printf "blah" fi
i have tried without double quotes.
the simplest should rely on ls
return value (it returns non-zero when files not exist):
if ls /path/to/your/files* 1> /dev/null 2>&1; echo "files exist" else echo "files not exist" fi
i redirected ls
output make silent.
edit: since answer has got bit of attention (and useful critic remarks comments), here optimization relies on glob expansion, avoids use of ls
:
for f in /path/to/your/files*; ## check if glob gets expanded existing files. ## if not, f here pattern above ## , exists test evaluate false. [ -e "$f" ] && echo "files exist" || echo "files not exist" ## needed know, can break after first iteration break done
this similar @grok12's answer, avoids unnecessary iteration through whole list.
Comments
Post a Comment