- Check If the File Exists in Bash
- File Test Operators
- Different Examples to Check Whether the File Exists or Not
- Example 1: Check the Existence of the File Using the -F Operator with Single Third Brackets ([])
- Example 2: Check the Existence of the File Using the -F Operator with Double Third Brackets ([[ ]])
- Example 3: Check the Existence of the File Using the -F Operator with the “Test” Command
- Example 4: Check the Existence of the File with the Path
- Conclusion
- About the author
- Fahmida Yesmin
- Check if file exists [BASH]
- 4 Answers 4
- How to Check if File or Directory Exists in Bash Shell
- Check if file exists in bash script
- Check file exists in bash with test
- Check if file doesn’t exist in bash script
- Check if directory exists in bash script
- Check if directory doesn’t exist in bash
Check If the File Exists in Bash
Different types of files are used in Bash for different purposes. Many options are available in Bash to check if the particular file exists or not. The existence of the file can be checked using the file test operators with the “test” command or without the “test” command. The purposes of different types of file test operators to check the existence of the file are shown in this tutorial.
File Test Operators
Many file test operators exist in Bash to check if a particular file exists or not. Some of them are mentioned in the following:
Operator | Purpose |
-f | It is used to check if the file exists and if it is a regular file. |
-d | It is used to check if the file exists as a directory. |
-e | It is used to check the existence of the file only. |
-h or -L | It is used to check if the file exists as a symbolic link. |
-r | It is used to check if the file exists as a readable file. |
-w | It is used to check if the file exists as a writable file. |
-x | It is used to check if the file exists as an executable file. |
-s | It is used to check if the file exists and if the file is nonzero. |
-b | It is used to check if the file exists as a block special file. |
-c | It is used to check if the file exists as a special character file. |
Different Examples to Check Whether the File Exists or Not
Many ways of checking the existence of the regular file are shown in this part of the tutorial.
Example 1: Check the Existence of the File Using the -F Operator with Single Third Brackets ([])
Create a Bash file with the following script that takes the filename from the user and check whether the file exists in the current location or not using the -f operator in the “if” condition with the single third brackets ([]).
echo -n «Enter the filename: «
#Check whether the file exists or not using the -f operator
The script is executed twice in the following script. The non-existence filename is given in the first execution. The existing filename is given in the second execution. The “ls” command is executed to check whether the file exists or not.
Example 2: Check the Existence of the File Using the -F Operator with Double Third Brackets ([[ ]])
Create a Bash file with the following script that takes the filename as a command-line argument and check whether the file exists in the current location or not using the -f operator in the “if” condition with the double third brackets ([[ ]]).
#Take the filename from the command-line argument
#Check whether the argument is missing or not
#Check whether the file exists or not using the -f operator
if [ [ -f » $filename » ] ] ; then
The script is executed twice in the following script. No argument is given in the first execution. An existing filename is given as an argument in the second execution. The “ls” command is executed to check whether the file exists or not.
Example 3: Check the Existence of the File Using the -F Operator with the “Test” Command
Create a Bash file with the following script that takes the filename as a command-line argument and check whether the file exists in the current location or not using the -f operator with the “test” command in the “if” condition.
#Take the filename from the command-line argument
#Check whether the argument is missing or not
echo «No argument is given.»
#Check whether the file exists or not using the -f operator
if test -f » $filename » ; then
The script is executed twice in the following script. No argument is given in the first execution. An existing filename is given in the second execution.
Example 4: Check the Existence of the File with the Path
Create a Bash file with the following script that checks whether the file path exists or not using the -f operator with the “test” command in the “if” condition.
#Set the filename with the directory location
#Check whether the file exists or not using the -f operator
if test -f » $filename » ; then
The following output appears after executing the script:
Conclusion
The methods of checking whether a regular file exists or not in the current location or the particular location are shown in this tutorial using multiple examples.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
Check if file exists [BASH]
Any ideas please? I will be glad for any help. P.S. I wish I could show the entire file without the risk of being fired from school for having a duplicate. If there is a private method of communication I will happily oblige. My mistake. Fas forcing a binary file into a wrong place. Thanks for everyone’s help.
4 Answers 4
Little trick to debugging problems like this. Add these lines to the top of your script:
The set -xv will print out each line before it is executed, and then the line once the shell interpolates variables, etc. The $PS4 is the prompt used by set -xv . This will print the line number of the shell script as it executes. You’ll be able to follow what is going on and where you may have problems.
Here’s an example of a test script:
#! /bin/bash export PS4="\$LINENO: " set -xv FILE1="$" # Line 6 if [ ! -e "$FILE1" ] # Line 7 then echo "requested file doesn't exist" >&2 exit 1 else echo "Found File $FILE1" # Line 12 fi
And here’s what I get when I run it:
$ ./test.sh .profile FILE1="$" 6: FILE1=.profile if [ ! -e "$FILE1" ] then echo "requested file doesn't exist" >&2 exit 1 else echo "Found File $FILE1" fi 7: [ ! -e .profile ] 12: echo 'Found File .profile' Found File .profile
Here, I can see that I set $FILE1 to .profile , and that my script understood that $ . The best thing about this is that it works on all shells down to the original Bourne shell. That means if you aren’t running Bash as you think you might be, you’ll see where your script is failing, and maybe fix the issue.
I suspect you might not be running your script in Bash. Did you put #! /bin/bash on the top?
script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE
You may want to use getopts to parse your parameters:
#! /bin/bash USAGE=" Usage: script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE " while getopts gpr:d: option do case $option in g) g_opt=1;; p) p_opt=1;; r) rfunction_id="$OPTARG";; d) dfunction_id="$OPTARG";; [?]) echo "Invalid Usage" 1>&2 echo "$USAGE" 1>&2 exit 2 ;; esac done if [[ -n $rfunction_id && -n $dfunction_id ]] then echo "Invalid Usage: You can't specify both -r and -d" 1>&2 echo "$USAGE" >2& exit 2 fi shift $(($OPTIND - 1)) [[ -n $g_opt ]] && echo "-g was set" [[ -n $p_opt ]] && echo "-p was set" [[ -n $rfunction_id ]] && echo "-r was set to $rfunction_id" [[ -n $dfunction_id ]] && echo "-d was set to $dfunction_id" [[ -n $1 ]] && echo "File is $1"
How to Check if File or Directory Exists in Bash Shell
Here are several ways you can check if file or directory exists in bash shell script. You’ll also learn to check if file doesn’t exist.
If you are working on a Bash script that interacts with files and directories, you might encounter a situation where you need to make sure that the file or directory exists. This helps avoiding possible errors for performing certain actions on a file that doesn’t exist.
In this tutorial, I’ll show you a couple of ways to check if file or directory exists in bash script or not. Let’s start with file first.
Check if file exists in bash script
The idea here is to use the -f operator that returns true only when it is a regular file (not directory).
Let’s say you want to check if file /home/user/my_file exists or not. Here’s how you can check with square brackets
#!/bin/bash if [ -f /home/user/my_file ] then echo "My file exists" fi
But you won’t always get the file name before hand, will you? You can have it in a variable and if that’s the case, you can use it in this fashion.
#!/bin/bash FILE=/home/user/my_file if [ -f "$FILE" ] then echo "My file exists" else echo "My file doesn't exist" fi
Basically, what matters is the condition you use in the if command. It’s up to you how you want to use the if statement.
For example, you can write it with two square brackets, keep ‘then’ in the same line as ‘if’ with the help of semicolon like this:
#!/bin/bash FILE=/home/user/my_file if [ -f "$FILE" ]; then echo "My file exists" else echo "My file doesn't exist" fi
or put the entire statement together like this:
[ -f /home/user/my_file ] && echo "My file exists" || echo "My file doesn't exist"
Check file exists in bash with test
You can also use test in bash to see if file exists or not. It’s pretty much the same thing only that you don’t use the square brackets in the if statement:
#!/bin/bash FILE=/home/user/my_file if test -f "$FILE" then echo "My file exists" else echo "My file doesn't exist" fi
You can also use the above code in single line like this:
test -f /home/user/my_file && echo "My file exists" || echo "My file doesn't exist"
Check if file doesn’t exist in bash script
What if it’s the other way round and you want to check if file does not exist in bash? You can use pretty much the same code as above by using the negation operator:
#!/bin/bash FILE=/home/user/my_file if [ ! -f "$FILE" ] then echo "My file doesn't exist" fi
Now that you know how to deal with files, let’s move on to directories.
Check if directory exists in bash script
The code for checking directory is the same as the one you saw in the previous section. The only difference is that you’ll be using -d instead of -f. -d returns true only for directories.
#!/bin/bash if [ -d /home/user/my_dir ] then echo "My directory exists" fi
You can also use test here:
#!/bin/bash DIR=/home/user/my_dir if test -d "$DIR" then echo "My directory exists" else echo "My directory doesn't exist" fi
Check if directory doesn’t exist in bash
You can use the negation again to check if directory doesn’t exist:
#!/bin/bash DIR=/home/user/my_dir if [ ! -d "$DIR" ] then echo "My directory doesn't exist" fi
That’s it. That’s all you need to do for checking if a directory of file exists in bash shell or not.
I hope you find this bash tip useful. If you have any questions or suggestions, please feel free to leave a comment below.