How Do I Write a Script to Test if a File Exists?

Problem scenario
You want a script to test if a file is in a specific location or not. You are using Linux. What should you do?

Solution
Use this script (but modify "/path/to/file" to be the path and name of the file you are searching for):

if [ -f "/path/to/file" ]
then
echo "file exists"
else
echo "file does not exist"
fi

Leave a comment

Your email address will not be published. Required fields are marked *