The Wayback Machine - https://web.archive.org/web/20240808003844/https://www.geeksforgeeks.org/bash-scripting-how-to-check-if-file-exists/
Open In App

Bash Scripting – How to check If File Exists

Last Updated : 16 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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

Syntax :

  • test [expression]
  • [ expression ]
  • [[ expression ]]

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

  • –f:  It returns True if the file exists as a common ( regular ) file.
  • -d: it returns True if directory exists.
  • -e: It returns True if any type of file exists.
  • -c: It returns True if the character file exists.
  • -r: It returns True if a readable file exists.
  • –w: It returns True if a writable file exists.
  • -x: It returns True if an executable file exists.
  • -p: It returns True if the file exists as a pipe.
  • -S: It returns True if the file exists as a socket.
  • -s: it returns True if a file exists and the size of the file is not zero.
  • -L: It returns True if the file of symbolic link exists.
  • -g: It returns True if the file exists and hold set group id flag is set..
  • -G: It returns True if the file exists and holds the same group id that is in process.
  •  -k: It returns True if the file exists and the sticky bit flag is set.

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

  • -ef: It returns True if both files exist and indicate the same file. 

Example : 

FirstFile -ef SecondFile
  • -nt: It returns True if FirstFile is newer than Secondfile.

Example :

FirstFile -nt FileOld
  • -ot: It returns True if FirstFile is older than SecondFile.

Example: 

FirstFile -ot SecondFile

Let us take some examples based on syntax :

  • [ expression ]: First, create a file named ” FirstFile.sh ” and write the following script on it
#!/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 “.

  • test [expression]: Now, modify the above script in ” FirstFile.sh ” as follows
#!/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 “.

  • [[ expression ]]: Again modify the above script in ” FirstFile.sh ” as follows
#!/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:

  • Using -d parameter: Create a file named ” FirstDir.sh ” and write the following script in it
!/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 :

  • Using -nt parameter

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 “.



Similar Reads

Bash Scripting - Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to a
10 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we print or echo some text on screen with the help of a t
2 min read
Bash Scripting - Working of Bash Scripting
Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and ot
6 min read
Bash Scripting - Write Output of Bash Command into Log File
Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file. Let see how to write the output of the bash command into a log file.
4 min read
Bash Scripting - Bash Read Password without Echoing back
Generally, you can see on the login page, whenever we type a password either it show dot (•) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And in some cases, a dot or asterisk also may not appe
3 min read
Bash Scripting - Difference between Zsh and Bash
A shell is an environment in which various commands can be executed, it provides an interface between the user and the UNIX system. Basically, a shell is a command-line interpreter which interprets the commands given by the user, it can also read the combination of such commands which is known as a shell script. The shell provides us with an enviro
2 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally three options:-n: It does not print the trailing n
2 min read
Bash Scripting - How to read a file line by line
In this article, we are going to see how to read a file line by line in Bash scripting. There might be instances where you want to read the contents of a file line by line using a BASH script. In this section, we will look at different ways to do just that. We will use BASH commands and tools to achieve that result. We make use of the read and cat
3 min read
Bash Scripting - File Extension
Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file name. For example, in the file name "file.txt", the
6 min read
Bash Scripting - How to check If variable is Set
In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value.  In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Using the "-v" optionThe -v option allows us to check
10 min read
Article Tags :
three90RightbarBannerImg