How to check if a directory exists in a given path

I am creating a simple backup script and I want to check if the directory exists in the specified path:

fullPath=/usr/local/webapps/mydir if mydir exists in my $fullPath then back it up else give error fi 

My question is: how to formulate an if statement to check if the last directory exists in the $ fullPath variable? To clarify, for this case it would be mydir , if I used / usr / local, it would be local , etc.

Thanks in advance!

+7
source share
1 answer

Duplicate question? Check if directory exists in shell script

 if [ -d "$DIRECTORY" ]; then # Control will enter here if $DIRECTORY exists. fi 

$ DIRECTORY is the way to go

In your case, you can simply do:

 if [ -d "$fullPath" ]; then then back it up else give error fi 
+16
source

All Articles