Suppose I'm in a directory and I know a file archivo
exists:
if [ -f "archivo" ]; then
echo "archivo existe"
fi
Is there a way to check if the file doesn't exist without using else
this conditional?
Suppose I'm in a directory and I know a file archivo
exists:
if [ -f "archivo" ]; then
echo "archivo existe"
fi
Is there a way to check if the file doesn't exist without using else
this conditional?
Extracted from sister question How do I tell if a regular file does not exist in bash? .
Yes! You just have to use the negation
!
in the conditional:Or in a more concise way:
Note that it
man test
indicates that the option-f
:So the negation of this simply means that what is indicated is not a regular file. It may be nothing or it may be a directory, etc.
You can use the negation
!
before the-f
or before
[
It can also be used
-e
this wayThis is the script I use to check if a file exists:
the option
-f
is negated to determine the file does not exist.