Search code examples
linuxfilepattern-matchingunary-operator

Linux unary operator to check exitence of file with pattern name


I´d like to check if there is any file whose name matches a given pattern, in a given path.

assert=1
myPatternpath=$HOME/folderName/abcMYPATTERN.xml
if [ ! -f $myPatternpath ] 
then assert=0 
fi
echo $assert;

MYPATTERN should be any character.

Edited:

MYPATTERN can be for example: *

Thanks


Solution

  • So, that's not a regex you want, but a wildcard. In that case, you could do something like:

    for file in $HOME/folderName/*.xml; do
        if [ ! -f $file ] then assert=0; fi
    done
    echo $assert;
    

    But of course, beacuse you are going through the files, the -f is implied. This would still be useful if checking for things like whether it's a folder, readable, etc...