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?

1
  • 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, 2015 at 13:29

4 Answers 4

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

2
  • 2
    This answer does not seem to be correct. mkdir indeed emits an error if the directory exists, unless using the -p flag. Jul 19, 2016 at 2: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 2, 2018 at 6: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.

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.

4
  • This seems like more of a comment than an answer. Feb 15, 2020 at 9: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." Feb 15, 2020 at 9: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. Oct 19, 2021 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 Oct 19, 2021 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .