Is mkdir -p totally safe when creating folder already exists
Solution 1
mkdir -p
would not give you an error if the directory already exists and the contents for the directory will not change.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Related videos on Youtube
![Tommy](https://lh3.googleusercontent.com/-DUshWzaXQHs/AAAAAAAAAAI/AAAAAAAADXw/4eCJN41Q1wQ/photo.jpg?sz=256)
Tommy
Something for nothing. Bite me if you can score 9+ in a CPS Test.
Updated on September 18, 2022Comments
-
Tommy almost 2 years
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?
-
Marco over 8 yearsThe 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.
-
-
Aaron Cicali almost 8 yearsThis answer does not seem to be correct. mkdir indeed emits an error if the directory exists, unless using the -p flag.
-
Kunal Pal almost 6 yearsin error, you could check for the code like this
if(err.code == 'EEXIST')
this condition will get true if the directory already exists. -
G-Man Says 'Reinstate Monica' over 4 yearsThis seems like more of a comment than an answer.
-
roaima over 4 yearsI 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." -
Charlie Reitzel over 2 yearsNothing "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 withmkdir: cannot create directory '/etc/foo': File exists
. So, at least on my older version of SuSE Linux,mkdir -p
will not follow symbolic links. -
roaima over 2 years@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