Example 01
We have to add the Bash support in it as ‘#!/bin/bash’. After this, we have initialized a ‘FILE’ variable with the path to the file itself ‘file.sh’ i.e., ‘/home/linuxhint/file.sh’. To check for the file exists or not, we will be using the ‘If-then’ statement taking its conditions in the single square brackets in this Bash code. To check whether the file exists, we need to use the ‘-f’ option followed by the double-quoted variable “FILE” with the dollar character “$”. This is to check whether the given path to the file contains a file with the name ‘file.sh’ or not.
The condition ends here and the ‘then’ part of the statement starts. If the file exists, it will display the message that the file ‘file.sh’ exists in the given path using the ‘echo’ statement. The ‘if’ statement ends here with the ending syntax ‘fi’.
FILE=/home/linuxhint/file.sh
if [ -f "$FILE" ]; then
echo "$FILE exists!"
fi
Now, save your Bash code file and run file using the command line below:
Here is the output when running the command:
/home/linuxhint/file.sh exists!
If we tweak the script and change the ‘FILE’ to a file that doesn’t exist and rerun the script we get no output from the script.
Example 02
In this example we will add an else statement to the if statement in the previous example. That way if the file does not exist we will get an appropriate error message printed to the standard output or screen. Here is the new code:
FILE=/home/linuxhint/file.sh
if [ -f "$FILE" ]; then
echo "$FILE exists!"
else
echo "$FILE does not exist!"
fi
We can run the scripts as follows:
We get the same output as before:
/home/linuxhint/file.sh exists!
But now if we hack the filename to be a file that does not exist we will get a proper error message:
/home/linuxhint/nofile.sh does not exist!
Example 03
Let’s say, you want to use the ‘not’ character so you are firstly checking if the file does not exist. For example if the file does not exist you may want to print an error message, but you may not even require an else in this context as you only care if the file doesn’t exist, otherwise the script may continue as normal without exiting an error.
Here is the syntax to use the ‘!’ character to reverse the check to check if the file does not exist:
FILE=/home/linuxhint/nofile.sh
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist!"
else
echo "$FILE exists!"
fi
We will first run the script with the broken filename:
The output when it triggers the error condition is here below:
/home/linuxhint/nofile.sh does not exist!
Now we can fix the filename to be the file that exists:
FILE=/home/linuxhint/file.sh
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist!"
else
echo "$FILE exists!"
fi
Here is the output with the file that exists:
/home/linuxhint/file.sh exists!
linuxhint@u22:~$
Example 04
In this example we will show a quick syntax from the shell without writing a script and introduce logical operators to print success or errors. Here is the script you can run on the terminal:
file exists
linuxhint@u22:~$
In the above code we write the code directly on the interactive terminal. We use a conditional test for checking if the file exists. It will return 0 for success. Logical OR and Logical AND operators are used to indicate which message should be printed, success or failure.
Example 05
You can also utilize a similar approach as shown in this article to check for the existence of a directory using the ‘-d’ option in its condition instead of ‘-f’. Let’s say, we have a directory ‘test’ in our home folder and we have been using the same script with the ‘-d’ option to check for it and display the string message according to the condition output.
Here is the code with the directory check in file ‘dir.sh’:
FILE=/home/linuxhint/test
if [ -d "$FILE" ]; then
echo "directory $FILE exists!"
else
echo "directory $FILE does not exist!"
fi
To run the code execute:
directory /home/linuxhint/test exists!
And the negative test code and output:
FILE=/home/linuxhint/test
if [ ! -d "$FILE" ]; then
echo "directory $FILE does not exist!"
else
echo "directory $FILE exists!"
fi
directory /home/linuxhint/test does not exist!
Conclusion
This is all about the use of bash scripts to check if files and directories exist. Also, we have used the direct code in the Bash console utilizing test operations and logical operators.