Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if a regular file does not exist in Bash?

I've used the following script to see if a file exists:

#!/bin/bash  FILE=$1      if [ -f $FILE ]; then    echo "File $FILE exists." else    echo "File $FILE does not exist." fi 

What's the correct syntax to use if I only want to check if the file does not exist?

#!/bin/bash  FILE=$1      if [ $FILE does not exist ]; then    echo "File $FILE does not exist." fi 
like image 382
Bill the Lizard Avatar asked Mar 12 '09 14:03

Bill the Lizard


People also ask

How do you know if a file is regular or not?

How can I tell if a file is regular? In bash, the command "test -f file" returns an exit status of 0 if file is a regular file. It returns 1 if file is of another type or does not exist. /etc/passwd is a regular file.

How do you check if a file exists in Terminal Linux?

The best Linux command to check if a file Exists in bash is using the if statement -e option. The -e option is a built-in operator in Bash to check file exists. If the file exists, this command will return a 0 exit code. If the file does not exist, it will return a non-zero exit code.


1 Answers

The test command ([ here) has a "not" logical operator which is the exclamation point (similar to many other languages). Try this:

if [ ! -f /tmp/foo.txt ]; then     echo "File not found!" fi 
like image 151
John Feminella Avatar answered Sep 18 '22 06:09

John Feminella