Checking if a file exists in a shell script

To check if a file exists in a shell script, you need to you the -f option. Like this,

if [ -f /dir1/dir2/file.txt ]
then
 # do something
fi
This is for the sh shell, other shells my use a different method.

Leave a Reply

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