Site icon LinuxCommands.site

mkdir if not exists in linux/unix

mkdir creates a directory if it does not exist. We can use the mkdir -p option.

mkdir -p option

mkdir -p syntax

mkdir -p foo/bar/baz

will create directories foo, foo/bar, and foo/bar/baz if they don’t exist.

example

In the following example, we will use the mkdir command -p option to create a multi-level directory that does not exist.

➜  mkdir -p foo/bar/baz

We execute the “mkdir -p foo/bar/baz” command again. You will find that the command will not report any errors.

Next we use the mkdir -p option to create a directory named “baz2” under the directory “foo/bar“.

➜  mkdir -p foo/bar/baz2

ok. Created successfully.

So, when we do not confirm whether the directory exists, we can use the mkdir -p option to create it when the directory does not exist.

Exit mobile version