How to tell if a regular file does not exist in bash?

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 does not exist without having to use the else of this conditional?

 14
Author: Pablo, 2015-12-01

3 answers

extracted from sister question How do I tell if a regular file does not exist in bash?.

Yes! Just use the negation ! in the conditional:

if [ ! -f "archivo" ]; then
    echo "archivo no existe"
fi

Or more concisely:

[ ! -f "archivo" ] && echo "archivo regular no existe"

Note that man test indicates that the option -f:

-f FILE
     FILE exists and is a regular file

So the negation of this simply means that the indicated is not a regular file. It can be nothing or it can be a directory, etc.

 13
Author: fedorqui 'SO deja de dañar', 2017-05-23 12:39:20

You can use the negation ! before the -f

if [ ! -f "$archivo" ]; then
    echo "archivo inexistente"
fi

Or before [

if ! [ -f "$archivo" ]; then
    echo "archivo inexistente"
fi

You can also use -e this way

if [ ! -e "$archivo" ]; then
   echo "archivo inexistente"
fi
 9
Author: Alan, 2015-12-02 11:07:35

This is the script I use to check if a file exists:

if [ ! -f "/miFolder/miArchivo.txt" ]; then
    echo "El archivo no existe"
fi

The -f option is denied to determine the file does not exist.

 5
Author: Jorgesys, 2017-06-05 16:21:28