Thursday, 29 January 2009

Linux/UNIX: Find Out If File Exists With Conditional Expressions

This snippet shows you the script used to find if a file exists.

The script works as follows:

First it we declare the Variable FILE to equal what ever you specify on the command line when you execute the script.
Next step is the test function, we say if (-f) the $FILE (you entered) exists then,
print on the screen file exists else print it does not.

#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo \"File $FILE exists\"
else
echo \"File $FILE does not exists\"
fi

1 comment:

  1. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.

    Bry
    www.gofastek.com

    ReplyDelete