how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind is something like this:

cd /path/.../
if [file does NOT exists]
echo "/path/.../" >> bad.txt
fi

How would I check and see if a file exists? Thanks for any help!

Try:

if [ ! -f file ]; then
echo "/path/.../" >> bad.txt
fi
1 Like

Thank you, that works!
One slight problem however. When I run the code from the parent directory, and say: (edited to be more specific)

#!/bin/ksh

cd /path/
if [ ! -f file ]; then
echo "/path/" >> bad.txt
fi

I get no output. It appears that for some reason it's not changing to the directory and running the if statement, but rather running the if statement in the parent directory. How would I go about fixing this?

---------- Post updated at 05:01 PM ---------- Previous update was at 04:52 PM ----------

Never mind, figured it out :slight_smile: