Tom
Tom

Reputation: 11

How do I check for file existence in Unix?

How do you check if any file (the user chooses) exists in a certain place?

$org is a variable that I chose to set a path

if [ -e $org/$1]
then
echo "Do you want to overwrite"
exit 1

Do I use the if statement or getopts?

Upvotes: 1

Views: 468

Answers (2)

Mohammad Javad Naderi
Mohammad Javad Naderi

Reputation: 506

There should be a space between $org/$1 and ].

Try this:

if [ -e "$org/$1" ]; then
    echo "Do you want to overwrite"
fi

More information here: http://tldp.org/LDP/abs/html/fto.html

Upvotes: 2

mouviciel
mouviciel

Reputation: 67831

-f checks if the path exists and is a file

-d checks if the path exists and is a directory

Upvotes: 2

Related Questions