Search code examples
linuxshellmkdir

mkdir -p fails when directory exists


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.


Solution

  • 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.