Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a files exists in a specific directory in a bash script?

This is what I have been trying and it is unsuccessful. If I wanted to check if a file exists in the ~/.example directory

FILE=$1
if [ -e $FILE ~/.example ]; then
      echo "File exists"
else
      echo "File does not exist"
fi
like image 531
Bob Avatar asked Apr 28 '15 18:04

Bob


1 Answers

You can use $FILE to concatenate with the directory to make the full path as below.

FILE="$1"
if [ -e ~/.myexample/"$FILE" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi
like image 113
Eric Renouf Avatar answered Sep 29 '22 07:09

Eric Renouf