43

Say I have a folder:

./folder/

Inside it there are many files and even sub-directories.

When I execute:

mkdir -p folder

I won't see any errors even warnings.

So just want to confirm, is there anything lost or changed in result of this command?

AGamePlayer
  • 7,605
  • 2
    The help of mkdir says that the directories are only created when they are not present. This implies to me that when the directory exists there is nothing done. – Marco Nov 14 '15 at 13:29

4 Answers4

58

mkdir -p would not give you an error if the directory already exists and the contents for the directory will not change.

Manual entry for mkdir

ARG
  • 2,003
  • 19
  • 12
  • 2
    This answer does not seem to be correct. mkdir indeed emits an error if the directory exists, unless using the -p flag. – Aaron Cicali Jul 19 '16 at 02:35
  • 1
    in error, you could check for the code like this if(err.code == 'EEXIST') this condition will get true if the directory already exists. – Kunal Pal Aug 02 '18 at 06:53
15

A portable script will rely upon POSIX, which says of mkdir's -p option:

Each dir operand that names an existing directory shall be ignored without error.

and if there are no errors reported, the -p option has done its job:

Create any missing intermediate pathname components.

Thomas Dickey
  • 76,765
11

mkdir WILL give you an error if the directory already exists.

mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.

2

You say that,

When I execute mkdir -p folder I won't see any errors even warnings.

You will see an error if the command fails. The -p flag only suppresses errors if the directory already exists.

touch x
mkdir -p x
mkdir: cannot create directory ‘x’: File exists

The same issue will occur if you try to create a directory as a normal user in, say, /etc.

What the -p will suppress are errors that would be triggered when the target directory already exists

mkdir y
mkdir -p y

However in all cases you won't lose anything, and nothing will be changed. In the error situations you just won't have the directory you were expecting.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • This seems like more of a comment than an answer. – G-Man Says 'Reinstate Monica' Feb 15 '20 at 09:43
  • I wondered about that, but I'm specifically rebutting the statement in the OP's question, "When I execute mkdir -p folder I won't see any errors even warnings." – Chris Davies Feb 15 '20 at 09:50
  • Nothing "lost or changed", but I have found a case where the command fails to create the directory when you might expect it to: mkdir -p /external/etc/foo; ln -s /etc/foo /external/etc/foo; mkdir -p /etc/foo/bar/biff. The 2nd mkdir -p fails with mkdir: cannot create directory '/etc/foo': File exists. So, at least on my older version of SuSE Linux, mkdir -p will not follow symbolic links. – Charlie Reitzel Oct 19 '21 at 23:06
  • @ChatlieReitzel I don't see your point. It doesn't really matter whether or not you expect an operation to work. The exit status will indicate either success or an error, so you can handle that appropriately. The -p flag only suppresses errors for a very small defined set it circumstances. Your example doesn't fit into that set so you get an error – Chris Davies Oct 19 '21 at 23:31