HomeLinuxExamine If the File Exists in Bash

Examine If the File Exists in Bash


Several types of information are utilized in Bash for various functions. Many choices can be found in Bash to verify if the actual file exists or not. The existence of the file will be checked utilizing the file take a look at operators with the “take a look at” command or with out the “take a look at” command. The needs of several types of file take a look at operators to verify the existence of the file are proven on this tutorial.

File Check Operators

Many file take a look at operators exist in Bash to verify if a specific file exists or not. A few of them are talked about within the following:

Operator Function
-f It’s used to verify if the file exists and if it’s a common file.
-d It’s used to verify if the file exists as a listing.
-e It’s used to verify the existence of the file solely.
-h or -L It’s used to verify if the file exists as a symbolic hyperlink.
-r It’s used to verify if the file exists as a readable file.
-w It’s used to verify if the file exists as a writable file.
-x It’s used to verify if the file exists as an executable file.
-s It’s used to verify if the file exists and if the file is nonzero.
-b It’s used to verify if the file exists as a block particular file.
-c It’s used to verify if the file exists as a particular character file.

Totally different Examples to Examine Whether or not the File Exists or Not

Some ways of checking the existence of the common file are proven on this a part of the tutorial.

Instance 1: Examine the Existence of the File Utilizing the -F Operator with Single Third Brackets ([])

Create a Bash file with the next script that takes the filename from the consumer and verify whether or not the file exists within the present location or not utilizing the -f operator within the “if” situation with the only third brackets ([]).

#!/bin/bash

#Take the filename

echo -n “Enter the filename: “

learn filename

#Examine whether or not the file exists or not utilizing the -f operator

if [ -f $filename ]; then

echo “File exists.”

else

echo “File doesn’t exist.”

fi

The script is executed twice within the following script. The non-existence filename is given within the first execution. The present filename is given within the second execution. The “ls” command is executed to verify whether or not the file exists or not.

Instance 2: Examine the Existence of the File Utilizing the -F Operator with Double Third Brackets ([[ ]])

Create a Bash file with the next script that takes the filename as a command-line argument and verify whether or not the file exists within the present location or not utilizing the -f operator within the “if” situation with the double third brackets ([[ ]]).

#!/bin/bash

#Take the filename from the command-line argument

filename=$1

#Examine whether or not the argument is lacking or not

if [ $filename != “” ]; then

#Examine whether or not the file exists or not utilizing the -f operator

if [[ -f $filename ]]; then

echo “File exists.”

else

echo “File doesn’t exist.”

fi

else

echo “Argument is lacking.”

fi

The script is executed twice within the following script. No argument is given within the first execution. An current filename is given as an argument within the second execution. The “ls” command is executed to verify whether or not the file exists or not.

Instance 3: Examine the Existence of the File Utilizing the -F Operator with the “Check” Command

Create a Bash file with the next script that takes the filename as a command-line argument and verify whether or not the file exists within the present location or not utilizing the -f operator with the “take a look at” command within the “if” situation.

#!/bin/bash

#Take the filename from the command-line argument

filename=$1

#Examine whether or not the argument is lacking or not

if [ $# -lt 1 ]; then

echo “No argument is given.”

exit 1

fi

#Examine whether or not the file exists or not utilizing the -f operator

if take a look at -f $filename; then

echo “File exists.”

else

echo “File doesn’t exist.”

fi

The script is executed twice within the following script. No argument is given within the first execution. An current filename is given within the second execution.

Instance 4: Examine the Existence of the File with the Path

Create a Bash file with the next script that checks whether or not the file path exists or not utilizing the -f operator with the “take a look at” command within the “if” situation.

#!/bin/bash

#Set the filename with the listing location

filename=‘temp/programs.txt’

#Examine whether or not the file exists or not utilizing the -f operator

if take a look at -f $filename; then

echo “File exists.”

else

echo “File doesn’t exist.”

fi

The next output seems after executing the script:

Conclusion

The strategies of checking whether or not an everyday file exists or not within the present location or the actual location are proven on this tutorial utilizing a number of examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments