How to check if file does not exist in Bash

You can check if a file does not exist in Bash by using the ! -f or ! -e operator. For example:

if ! [ -f "/path/to/file" ]; then
echo "File does not exist"
fi

Or

if [ ! -e "/path/to/file" ]; then
echo "File does not exist"
fi

Both of these commands will check if the file located at “/path/to/file” does not exist, and if it does not, the script will output “File does not exist”. The -f option checks if the file exists and is a regular file, and the -e option checks if the file exists regardless of type.

Leave a Comment