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?
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?
mkdir -p would not give you an error if the directory already exists and the contents for the directory will not change.
if(err.code == 'EEXIST') this condition will get true if the directory already exists.
– Kunal Pal
Aug 02 '18 at 06:53
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.
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.
You say that,
When I execute
mkdir -p folderI 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.
mkdir -p folder I won't see any errors even warnings."
– Chris Davies
Feb 15 '20 at 09:50
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
-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