I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir
command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir
throws when it tries to create an existing directory.
Any thoughts on how best to do this?
Defining complex directory trees with one command
mkdir foo
works even if the directory exists. To make it work only if the directory named "foo" does not exist, try using the-p
flag.Example :-
This will create the directory named "foo" only if it does not exist. :)
If you don't want to show any error message:
If you want to show your own error message:
The old tried and true
will do what you want with none of the race conditions many of the other solutions have.
Sometimes the simplest (and ugliest) solutions are the best.
Or if you want to check for existence first:
-e is the exist test for korn shell.
You can also try googling a korn shell manual.
This is a simple function (bash shell) which lets you create a directory if it doesn't exist.
You can call the above function as :
The above creates fooDir and BarDir if they don't exist. Note the "-p" option in mkdir command which creates directories recursively. Hope this helps.