What are file test operators in Bash Scripting

 File test operators in Bash Scripting 

What are file test operators in Bash Scripting

Table of contents:- 

  • Why do we need a file test operator?

  • Different types of file test operators

  • How to use file test operators

Why do we need a file test operator? 

Sometimes when you are using Bash Scripting you need to check for some files their permissions are they exists or not which type are etc, for these purposes we have file test operators. 

Different types of file test operators


-e --> Checks whether the file exists or not.

-f --> Checks whether the file exists and it is regular file or not.

-d --> Checks for the directory.

-b --> Checks whether the file is block special.

-c --> Checks whether the file is character special.

-s --> Checks whether the file is empty.

-r --> Checks whether the file has read permissions.

-w --> Checks whether the file has write permissions.

-x --> Checks whether the file has executable permissions.


Using file test operators in practical
We are going to take a look at using file test operators by actually using them by writing a simple bash script. 

  • Writing a simple Bash Scripting to check whether a file is there or not. 

#! bin/bash

# Writing a simple Bash Scripting to check whether a file is there or not.

read -p "Enter the name of the file:- " file_name

if [ -e $file_name ]
then
echo "$file_name found"
else
echo "$file_name not found"
fi


The above Script is just taking a file name as an input from the user & using the operator -e, which is used to check if the file exists or not, it's checking it and outputting the user according to it if the file exists or not, by using the if-else statements ( Conditional Statements in Bash Scripting )

What are file test operators in Bash Scripting

Initially, I don't have any file in my folder if I check for a file it should return a file not found, let's check. 


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter the name of the file:- test.txt
test.txt not found



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter the name of the file:- bashscript.sh
bashscript.sh found


As you can see when we give it a valid file name that exists in the folder then it gives using the output of the file found as we have set it to do that. 

Now you can use all the operators to check the existence of files & check the specific thing you want to check. 

An Interesting Bash Script 

  • Writing a Bash Script to check whether a file exists or not & if not create that. 

#! bin/bash

# Writing a Bash Script to check if the file exists or not & if not creating that

read -p "Enter the file name:- " file_name

if [ -e $file_name ]
then
echo "$file_name Found"

else
echo "$file_name not found"

read -p "do you want to create that file (Y/N)" create_or_not

if [ $create_or_not = 'y' ]
then
echo "Creating the file"
sleep 0.2
touch $file_name
sleep 0.3
echo "File Created"
else
echo "File not Created"
fi

fi


Sequential execution of the script 
  1. Taking a file name as input from the user. 

  2. Checking if the file exists ( if yes output file found )

  3. If the above evaluates to false ( output file not found )

  4. Take input from the user if they want to create that file or not 

  5. If the user states he wants to create, then just create a file 

  6. If not output file is not created & the execution of the program stops. 

Output:- 


┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter the file name:- testing.txt
testing.txt not found
do you want to create that file (Y/N)y
Creating the file
File Created



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter the file name:- test.txt
test.txt not found
do you want to create that file (Y/N)n
File not Created



┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter the file name:- testing.txt
testing.txt Found

As you can see above we ran the script the file didn't exist so it outputted the file not found, then it asked us if we wanted to create that file or not, and we said yes & it created the file. 

The second time we ran the script we said no to that and it outputted a file not created. 

The third time we rant the script we gave it the file name that already exists, then it outputted the file found. 

 Conclusion:- 

This is how we can use the file test operator in Bash Scripting for creating some cool scripts to make out work much easier than it was previously. 

Next Post Previous Post
No Comment
Add Comment
comment url