How to check if file does not exist in Bash

How can I check if a file does not exist in a Bash script?

We can quickly tell if a standard file does not exist in Bash using the test command or [ builtin. This page explains how to find a regular file under the Linux or Unix-like system using Bash.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash on Linux/Unix/macOS
Est. reading time 3 mintues

Checking if file does not exist in Bash

The test command check file types and compare values. The basic syntax is as follows:

[ -f filename ] 
test filename 
[ ! -f filename ]
! test filename

Syntax

The test command always exits with a status of 0 (true) or 1 (false) depending on the evaluation of EXPR. For example:
  1. -f filename (test -f filename) returns true if file exists and is a regular file.
  2. The ! (exclamation point) act as logical "NOT" operator.

In other words we can use the if command as follows:

if [ ! -f /path/to/file ]
then
    echo "File does not exist in Bash"
else
    echo "File found. Do something meaningful here"
fi

Examples

#!/bin/bash
src="/etc/host.ip.conf"
if [ ! -f /etc/demo.txt ]
then
	echo "File not found"
fi
 
# another syntax #
[ ! -f "$src" ] && echo "$src file missing" || echo "$src file found and now copying ..."

Next, run the .sh file shell script in Linux / UNIX:
chmod +x script.sh
./script.sh

How do I tell if two regular files do not exist in Bash?

Often you want to do something if multiple files are found. For this kind of operation, we use boolean operators:

  1. EXPR1 && EXPR2 It returns true if both EXPR1 AND EXPR2 are true.
  2. EXPR1 || EXPR1 The test command returns True if either EXPR1 OR EXPR1 is true.

For instance:

#!/bin/bash
# a list of required files 
f1="/etc/hosts"
f2="/etc/resolv.conf"
 
#
# Die if $f1 or $f2 is missing 
#
if [ ! -f "$f1"  ] ||  [ ! -f "$f2" ]
then
	echo "Required files are missing."
else
	echo "Let us build SFTP jail."
fi

File operators list

Table 1 - test command file and other operators list
Operator Returns
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
! EXPR Logical not.
EXPR1 && EXPR2 Perform the and operation.
EXPR1 || EXPR2 Perform the or operation.

Bash builtin vs external test

Please note that the test command is provided as both built-in (internal) and external commands. So syntax may vary. This example is using bash's builtin command. We can verify that using the test command or command command:
type -a test
# /bin/test and /usr/bin/test are same (hardlinks) files #
stat /usr/bin/test /bin/test
# find default test command #
command -V test

Summing up

You learned how to check if a file does not exist in Bash script. I recommend that you read GNU/BASH manual/documenation online or by tying the following man command:
man bash
help test # bulitin test help page
man test # external test man page


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Alf Jun 14, 2021 @ 14:43

    Thanks!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum