mkdir -p fails when directory exists

UmNyobe picture UmNyobe · Jan 24, 2013 · Viewed 43.2k times · Source

On one of our remote systems mkdir -p $directory fails when the directory exists. which means it shows

mkdir: cannot create directory '$directory' : file exists

This is really puzzling, as I believed the contract of -p was that is always succeed when the directory already exists. And it works on the other systems I tried.

there is a user test on all of these systems, and directory=/home/test/tmp.

Answer

Ryan Tse picture Ryan Tse · Jan 24, 2013

This could be caused if there is already a file by the same name located in the directory.

Note that a directory cannot contain both a file and folder by the same name on linux machines.