Kurt Peek Kurt Peek - 1 year ago 66
Bash Question

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,

, 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

However, this throws an error

[-e: command not found

when I try to run the resulting
script using the
command. What is wrong with the code?

Answer Source

Blank missing between bracket and -e.

if [ -e x.txt ]
    echo "ok"
    echo "nok"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download