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?
Use the -p flag.
Try
mkdir -p
:Note that this will also create any intermediate directories that don't exist; for instance,
will create directories
foo
,foo/bar
, andfoo/bar/baz
if they don't exist.If you want an error when parent directories don't exist, and want to create the directory if it doesn't exist, then you can
test
for the existence of the directory first:This should work:
or:
which will create the directory if it doesn't exist, but warn you if the name of the directory you're trying to create is already in use by something other than a directory.
mkdir does not support -p switch anymore on Windows 8+ systems.
You can use this: