mkdir -p fails when directory exists
Check to see if there is a file (not a directory) with a name same as $directory.
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.