Open In App

Bash Scripting – How to check If File Exists

In this article, we will write a bash script to check if files exist or not. 

Syntax :

Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: –



Now, there are some more parameters for comparison between the two files.

Example : 



FirstFile -ef SecondFile

Example :

FirstFile -nt FileOld

Example: 

FirstFile -ot SecondFile

Let us take some examples based on syntax :

#!/bin/bash

# using [ expression ] syntax and in place 
# of File.txt you can write your file name 
if [ -f "File.txt" ]; 
then

# if file exist the it will be printed 
echo "File is exist"
else

# is it is not exist then it will be printed
echo "File is not exist"
fi

Now save and run the file using the following command

$ chmod +x ./FirstFile.sh
$ ./FirstFile.sh

Output : 

Output

Note: As the ” File.txt ” is present in the system. So, it printed ” File is exists “.

#!/bin/bash

# using test expression syntax and in place 
# of File2.txt you can write your file name
if test -f "File2.txt" ;
then

# if file exist the it will be printed 
echo "File is exist"
else

# is it is not exist then it will be printed
echo "File is not exist"
fi

Now, again save and run the file using the following command

$ chmod +x ./FirstFile.sh
$ ./FirstFile.sh

Output :

Output

Note: As the ” File2.txt ” is not present in the system. So, it printed ” File is not exist “.

#!/bin/bash

# using [[ expression ]] syntax and in place 
# of File3.txt you can write your file name
if test -f "File3.txt" ;
then

# if file exist the it will be printed 
echo "File is exist"
else

# is it is not exist then it will be printed
echo "File is not exist"
fi

Now, again save and run the file using the following command

$ chmod +x ./FirstFile.sh
$ ./FirstFile.sh

Output :

Output

Note: As the ” File3.txt ” is present in the system. So, it printed ” File is exist “.

Let us see an example based on parameters:

!/bin/bash
if [[ -d "GFG_dir" ]] ; # Here GFG_dir
 is directory and in place of GFG_dir you can write your Directory name
then
echo "Directory is exist" # If GFG_dir exist then it will be printed
else
echo "Directory is not exist" # If GFG_dir is not exist then it will be printed
fi

Now Save and run the file using the following command

$ chmod +x ./FirstDir.sh
$ ./FirstDir.sh

Output :

Output

Note: As the ” GFG_dir ” is present in the system. So, it printed ” Directory is exist “.

Similarly, you can use -f , -e , -w , -r , -c ,etc. ( according to their uses ) in place of -d for checking the existence of different types of files.

Let us see an example based on a comparison of two files :

Create a file name ” Comparison_File.sh ” and write the following script 

#!/bin/bash

# New_file.txt and Old_File.txt are names of two files.
if [[ "New_File.txt" -nt  "Old_File.txt" ]] ;
then

# This will be printed if Condition is true
echo "New_File.txt is newer than Old_File.txt" 
else

# This will be printed if Condition is False
echo "New_File.txt is not newer than Old_File.txt"
fi

Now Save and run the file using the following command

$ chmod +x ./Comparison_File.sh
$ ./Comparison_File.sh

Output :

Output

Note: As both files are present in the system and ” New_File.txt ” is newer than ” Old_File.txt

Let us see the example ” Check if File does not exist” :

Create a file named ” Check_Exist.sh ” and write the following script in it 

#!/bin/bash

# using ! before -f parameter to check if 
# file does not exist
if [[ ! -f "GFG.txt" ]] ;
then

# This will printed if condition is True
echo "File is not exist"
else

# This will be printed if condition is False
echo "File is exist"
fi

Now Save and run the file using the following command

$ chmod +x ./Check_Exist.sh
$ ./Check_Exist.sh

Output :

Output

Note:  ” GFG.txt ” is not present in the system. So, it will print “File is not exist”

Let us see an example without using the If-else  condition :

Create a file named ” Geeks_File.sh ” and write the following script in it

#!/bin/bash

# If File exist then first statement will be
# printed and if it is not exist then 2nd
# statement will be printed.
[ -f "GFG_File.txt" ] && echo "File is exist" || echo "File is not exist"

Now Save and run the file using the following command

$ chmod +x ./Geeks_File.sh
$ ./Geeks_File.sh

Output : 

Output

Note: As the ” GFG_File.txt ” is present in the system. So, it printed ” File is exist “.

Article Tags :