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.
How can I best do this?
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/bazif they don’t exist.Some implementation like GNU
mkdirincludemkdir --parentsas a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial Unixes, so it should be avoided.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
testfor the existence of the directory first: