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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s