- Check if file exists [BASH]
- 4 Answers 4
- How can I check a file exists and execute a command if not?
- 7 Answers 7
- How to check if a file exists in a shell script
- 7 Answers 7
- 3 ways to check if file exists in bash
- Check if a File Exists Using the test Command in bash
- Check if a File Exists Using if statement -e option in bash
- Check if a File Exists Using -f option in bash if statement
- File test operators in bash
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 can I check a file exists and execute a command if not?
I have a daemon I have written using Python. When it is running, it has a PID file located at /tmp/filename.pid . If the daemon isn’t running then PID file doesn’t exist. On Linux, how can I check to ensure that the PID file exists and if not, execute a command to restart it? The command would be
The «which has to be executed from a specific directory» part of your description sounds like a recipe for trouble. Beware — rethink if at all possible.
/tmp is a bad location to put PID files, since some distributions have cleaner processes that delete files from /tmp, and users may delete files from there to make space.
7 Answers 7
[ -f /tmp/filename.pid ] || python daemon.py restart
-f checks if the given path exists and is a regular file (just -e checks if the path exists)
the [] perform the test and returns 0 on success, 1 otherwise
the || is a C-like or , so if the command on the left fails, execute the command on the right.
So the final statement says, if /tmp/filename.pid does NOT exist then start the daemon.
@Barakuda — A non-regular file could be a directory, named pipe, network socket, character device, symbolic link.
I think && is if the condition is met, while || is if it’s not met. [ -f my_file ] || echo ‘absent’ && echo ‘exists’
test -f filename && daemon.py restart || echo "File doesn't exists"
if only the file needs to be checked for existence : test -f filename && echo «exists» || echo «does not exist»
If it is bash scripting you are wondering about, something like this would work:
if [ ! -f "$FILENAME" ]; then python daemon.py restart fi
A better option may be to look into lockfile
The other answers are fine for detecting the existence of the file. However for a complete solution you probably should check that the PID in the pidfile is still running, and that it’s your program.
Another approach to solving the problem is a script that ensures that your daemon «stays» alive.
Something like this (note: signal handling should be added for proper startup/shutdown):
$PIDFILE = "/path/to/pidfile" if [ -f "$PIDFILE" ]; then echo "Pid file exists!" exit 1 fi while true; do # Write it's own pid file python your-server.py ; # force removal of pid in case of unexpected death. rm -f $PIDFILE; # sleep for 2 seconds sleep 2; done
In this way, the server will stay alive even if it dies unexpectedly.
How to check if a file exists in a shell script
I’d like to write a shell script which checks if a certain file, archived_sensor_data.json , exists, and if so, deletes it. Following http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html, I’ve tried the following:
[-e archived_sensor_data.json] && rm archived_sensor_data.json
when I try to run the resulting test_controller script using the ./test_controller command. What is wrong with the code?
You must set one or more whitespace between opening square bracket «[» and option «-e» same as between filename and closing square bracket «]»
7 Answers 7
You’re missing a required space between the bracket and -e :
#!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi
I finally added two blank spaces, one after the opening square bracket and one before the closing one: [ -e archived_sensor_data.json ] && rm archived_sensor_data.json . The script seems to work now.
The main difference here is the fact that you are using «bash» scripting instead of «shell» scripting. Notice that the first line that you have added was #!/bin/bash, so you are telling the machine to use «bash» instead of sh. Because sh doesn’t recognize that argument «-e»
Here is an alternative method using ls :
(ls x.txt && echo yes) || echo no
If you want to hide any output from ls so you only see yes or no, redirect stdout and stderr to /dev/null :
(ls x.txt >> /dev/null 2>&1 && echo yes) || echo no
This code means: «if ls is successful, there is such file, otherwise, there is none». If ls failed, it does not mean that file is missing. It might be some other error. For example, create file in directory owned by root and try to do ls under regular user. It will fail with Permission denied , which is not equivalent that file does not exist.
The backdrop to my solution recommendation is the story of a friend who, well into the second week of his first job, wiped half a build-server clean. So the basic task is to figure out if a file exists, and if so, let’s delete it. But there are a few treacherous rapids on this river:
- Everything is a file.
- Scripts have real power only if they solve general tasks
- To be general, we use variables
- We often use -f force in scripts to avoid manual intervention
- And also love -r recursive to make sure we create, copy and destroy in a timely fashion.
Consider the following scenario:
We have the file we want to delete: filesexists.json
This filename is stored in a variable
:~/Documents/thisfolderexists filevariable="filesexists.json"
We also hava a path variable to make things really flexible
:~/Documents/thisfolderexists pathtofile=".." :~/Documents/thisfolderexists ls $pathtofile filesexists.json history20170728 SE-Data-API.pem thisfolderexists
So let’s see if -e does what it is supposed to. Does the files exist?
:~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0
However, what would happen, if the file variable got accidentally be evaluated to nuffin’
:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0
What? It is supposed to return with an error. And this is the beginning of the story how that entire folder got deleted by accident
An alternative could be to test specifically for what we understand to be a ‘file’
:~/Documents/thisfolderexists filevariable="filesexists.json" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 0
:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 1
So this is not a file and maybe, we do not want to delete that entire directory
man test has the following to say:
-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file . -h FILE FILE exists and is a symbolic link (same as -L)
3 ways to check if file exists in bash
In this blog post, we will discuss 3 different ways to check if a file exists in Bash. This is an important skill to have when you are working with files in a Linux environment. Each of these methods has its own benefits and drawbacks, so it is important to understand them all before deciding which one to use. Let’s get started!
The following Linux commands can be used to check if a file exists in bash.
- test -f /path/to/file && echo “FILE exists.” || echo “File does not exist”
- [ -e /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
- [ -f /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using the test Command in bash
The first method we will discuss is using the test command. This is a built-in command in Bash that can be used to test various things. In this case, we are interested in using it to check if a file exists. The syntax for this command is as follows:
If the file exists, this command will return a 0 exit code. If the file does not exist, it will return a non-zero exit code. So, we can use this command to check if a file exists like so:
if test -e /path/to/file; then
echo “File exists”
else
echo “File does not exist”
fi
We can do this in one command like this.
test -e /path/to/file && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using if statement -e option in bash
The best Linux command to check if a file Exists in bash is using the if statement -e option. The -e option is a built-in operator in Bash to check file exists. If the file exists, this command will return a 0 exit code. If the file does not exist, it will return a non-zero exit code.
The syntax for this operator is as follows:
if [ -e /path/to/file ] ; then
echo “File exists”
else
echo “File does not exist”
fi
We can do this in one command.
[ -e /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using -f option in bash if statement
The third method we will discuss is using the -f option in if statement. The -e option checks if the file path exists, while the -f option checks if the file path exists and if it is a regular file. The syntax for these operators are as follows:
if [-f /path/to/file ] ; then
echo “File exists”
else
echo “File does not exist”
fi
we can do this in one command line.
[ -f /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
File test operators in bash
The test command includes the following FILE operators that allow you to test for particular types of files:
- -d FILE FILE exists and is a directory.
- -e FILE FILE exists.
- -r FILE FILE exists and the read permission is granted.
- -s FILE FILE exists and it’s size is greater than zero (ie. it is not empty).
- -w FILE FILE exists and the write permission is granted.
- -x FILE FILE exists and the execute permission is granted.
As you can see, there are many different ways to check if a file exists in Bash. Each of these methods has its own benefits and drawbacks, so it is important to understand them all before deciding which one to use. In general, the “test” command is the simplest and most reliable way to check if a file exists. However, the other methods can be useful in certain situations. Thanks for reading!
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.