Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir -p fails when directory exists

Tags:

linux

shell

mkdir

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.

like image 591
UmNyobe Avatar asked Jan 24 '13 08:01

UmNyobe


People also ask

Will mkdir fail if directory exists?

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.

Why does mkdir not create directory?

mkdir: cannot create directory – Permission denied The reason for this error is that the user you're running the mkdir as, doesn't have permissions to create new directory in the location you specified. You should use ls command on the higher level directory to confirm permissions.


1 Answers

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.

like image 76
Ryan Tse Avatar answered Oct 08 '22 18:10

Ryan Tse