Directory exists

Hi all,

Sorry about this i'm sure this is a very silly question hence an easy answer but:

I'm trying to write a script, part of which I want to check if a directory exists, if it doesn't then create it.

Thanks for your help
Tez

-d option

search the forum.

From the manual for 'test':

[(1)                             User Commands                            [(1)

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       [ EXPRESSION ]
       [ OPTION

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

So,

MY_DIR="ZZZ"
if [ ! -d $MY_DIR ] ; then
  mkdir $MY_DIR
fi

Of course this will still fail if MY_DIR exists but is something other than a directory. So to prevent that you would use the -e option instead of -d.

That works thanks.