The focus of this guide is to discuss the existence of a file in your system through bash scripting:
How to check file existence using bash scripting:
1) By entering the file name in the terminal:
Firstly, we need to create a bash script file, use the below-mentioned command:
The name of the file I created is “testfile.sh”, the “.sh” extension indicates the shell script file:
Open the “testfile.sh” in any text editor. Then write the script, save it by pressing “save”.
One way is to find a file by asking for a filename from the user in the terminal.
Use “-f” to check the file’s existence.
Write the below script:
echo "Enter your filename"
read newfile1
if [ -f "$newfile1" ]
then
echo "File is found"
else
echo "File is not found"
fi
Go back to the terminal and run the file to print output:
Permission denied message would be displayed in the terminal.
Make it executable by executing the below-mentioned command:
Enter the file name, and it will print the output:
2) By entering the file name while writing the script:
Another way to find a file by giving the file name while writing the script. We have three ways to check the availability of the file. The first one is using the “test” command, the second is using “if” with an expression in square brackets, and the third is also with “if” but double square brackets as indicated below:
- “test EXPRESSION.”
- “if [ EXPRESSION ]”
- “if [[ EXPRESSION ]]”
Let’s understand it with examples:
1) test [ Expression ]
Copy the given script and paste it into the editor, save it:
filename=file1
if test -f "$filename";
then
echo "$file has found."
else
echo "$file has not been found"
fi
Output:
As there is no such file in my directory, therefore the code displays the “File is not found” message.
2) if [ Expression ]
Copy the following script to check the if file exists or not:
filename=myfile.txt
if [ -f "$filename" ];
then
echo "$filename has found."
else
echo "filename has not been found"
fi
Output:
3) if [[ Expression ]]
Copy the below-written script and paste it on the terminal:
filename=testfile
if [[ -f "$filename" ]];
then
echo "$filename has found."
else
echo "$filename has not been found"
fi
Output:
To check directory:
3) By entering the directory name while writing a script
Use the “-d” flag to check the existence of a directory.
In the below-mentioned script, “dir11” is the variable in which you store the file the one you are finding; in this example, I want to check directory name “testDir” exists or not.
dir11=testDir
if [ -d "$dir11" ]
then
echo "Directory has found"
else
echo "Directory has not been found"
fi
Output:
2) By entering the file name in the terminal:
When you run the command in the terminal to check whether the directory exists or not, you are required to enter the directory name that you are searching for:
echo "type your directory name."
read Dir1
if [ -d "Dir1" ]
then
echo "directory has been found"
else
echo "directory has not found"
fi
Output:
Checking the file without using the “if” statement:
The “test” command can be executed without the “if” statement. It will only display output if the file exists; else, there would be no output:
Write script:
-
test -f myfile.txt && echo "file has been found"
-
[ -f myfile.txt ] && echo "$file has been found."
-
[[ -f myfile.txt ]] && echo "$file has been found."
Output:
Checking the directory without using the “if” statement:
Use the below-mentioned statements to check a directory exists or not:
-
[[ -d testDir ]] && echo "directory does exist"
-
2) [ -d testDir ] && echo "directory does exist"
Output:
Checking multiple files/Directories:
1) Checking multiple files with “if” statements:
Use the “-a” flag to check the existence of various files instead of using nested “if/else” statements:
if [ -f new_file.txt -a -f newfile.txt ]; then
echo "Both files exist."
fi
Another way is:
if [[ -f new_file.txt && -f newfile.txt ]]; then
echo "Both files exist."
fi
Output:
2) Checking multiple files without using the “if” statement:
Use the following statement to check multiple files simultaneously 1without using “if”:
-
[[ -f new_file.txt && -f newfile.txt ]] && echo “Both files exits.”
-
[[ -f new_file.txt && -f newfile.txt ]] && echo “Both files exits.”
Output:
Conclusion:
This article has shown how to use bash scripting to check a file or a directory. We used different options to check the availability of a file. Firstly, we use the “test” command with different flags. Then we learned the usage of “if”, nested “if-else,” and without the “if” statements to check the file or directory. We also looked over how to check multiple files or directories.