Volodymyr Bezuglyy
Volodymyr Bezuglyy

Reputation: 16795

mkdir -p vs if [[ ! -d dirname ]]

Is there any reason to use

if [[ ! -d dirname ]]; then mkdir dirname; fi

instead of just

mkdir -p dirname

Upvotes: 1

Views: 1207

Answers (3)

javaPlease42
javaPlease42

Reputation: 4963

These two ksh commands are functionally the same since both will create a directory called dirname.

mkdir -p dirname is more elegant.

Upvotes: 0

Fredrik Pihl
Fredrik Pihl

Reputation: 45652

-d FILE        True if file is a directory.

-p             no error if existing, make parent directories as needed.

If dirname does not contain any parents then the two commands behave the same. However if dirname contains parents the -d will not create those. And [[ is shell-dependent.

Upvotes: 1

wormsparty
wormsparty

Reputation: 2499

The first syntax depends on the shell you are using, not the second.

Since both fail if dirname exists not as a directory, no, there's no difference.

Upvotes: 2

Related Questions