9

I'm creating a simple script for backing up and I want to check if a directory exists in a given path like so:

fullPath=/usr/local/webapps/mydir

if mydir exists in my $fullPath

    then back it up
    else give error
fi

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

Thanks in advance!

choroba
  • 231,213
  • 25
  • 204
  • 289
Mantas
  • 3,179
  • 4
  • 20
  • 32

1 Answers1

19

Duplicate question? Check if a directory exists in a shell script

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

The $DIRECTORY is the pathname

For your case, you can just do:

if [ -d "$fullPath" ]; then
    then back it up
    else give error
fi
Community
  • 1
  • 1
ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • 1
    Thank you sir, and sorry if it's duplicate I looked for 30min and couldn't find an answer. – Mantas Aug 21 '12 at 09:56