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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -