Check if File Does Not Exist in Bash
Fumbani Banda
Mar 24, 2022
Oct 24, 2021

This tutorial checks if a regular file does not exist in bash with the test
command.
Check if a file does not exist with test
in Bash
The test
command has a logical not operator, which is !
. We will also add the -f
option to specify a file. In this case, the condition tests if the file does not exist.
#!/bin/bash
if [ ! -f check_fil.sh ];
then
printf 'No such file\n'
else
printf 'File exists\n'
fi
Output:
No such file
Author: Fumbani Banda
Related Article - Bash File
- Write to a File in Bash
- Bash Overwrite File
- Open HTML File Using Bash
- Search for Files With a Filename Beginning With a Specified String in Bash
- Check if a File Is Empty in Bash
- Read File Into Variable in Bash