New Linux users often get puzzled by the “mkdir: cannot create directory” errors when taking first steps and trying to learn basics of working with files and directories. In this short post I’ll show the two most common types of this mkdir error and also explain how to fix things so that you no longer get these errors.
This should be self explanatory after a few weeks of using commands like mkdir, but the first time you see this it can be confusing.
File exists? How can it be when you’re just trying to create a directory? And why does it say “File exists” when you’re trying to create a directory, not a file?
This error suggests that the directory name you’re using (/tmp/try in my example shown on the screenshot) is already taken – there is a file or a directory with the same name, so another one can’t be created. You can use the wonderful ls command to check what’s going on:
Sure enough, we have a directory called /tmp/try already!
The reason it says “File exists” is because pretty much everything in Unix is a file. Even a directory!
Use the mv command to move /tmp/try into some new location (or giving it new name). Here’s how to rename /tmp/try into /tmp/oldtry:
Let’s rerun the mkdir command now:
…and since there are no errors this time, we probably have just created the /tmp/try directory, as desired. Let’s check both /tmp/try and the /tmp/oldtry with ls:
Another option you always have is to simply remove the file that’s blocking your mkdir command.
First, let’s create an empty file called /tmp/newtry and confirm it’s a file and not a directory usng ls command:
Now, if we try mkdir with the same name, it will fail:
So, to fix the issue, we remove the file and try mkdir again:
This time there were no errors, and ls command can show you that indeed you have a directory called /tmp/newtry now:
This is another very common error when creating directories using mkdir command.
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.
Let’s proceed with an example:
All of these commands succeeded because I first created new directory called try2018, then another subdirectory inside of it. ls command confirmed that I have 775 permissions on the try2018 directory, meaning I have read, write and execture permissions.
Now, let’s remove the write permissions for everyone for directory try2018:
If I try creating a subdirectory now, I will get the mkdir: cannot create directory – permissions denied error:
To fix the issue, let’s add write permissions again:
As you can see, try2018/yetanotherone directory was successfully created:
That’s it for today! Hope you liked this tutorial, be sure to explore more basic Unix tutorials on my blog.
See Also Basic Unix commands mkdir command in Unix File types in Unix chmod and chown Unix commands tutorial