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.
mkdir: cannot create directory – File exists
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:
greys@vps1:~$ ls -ald /tmp/try drwxr-xr-x 2 greys root 4096 Nov 5 18:55 /tmp/try
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!
Possible solutions to mkdir: cannot create directory – file exists scenario
Rename (move) existing 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:
greys@vps1:~$ mv /tmp/try /tmp/oldtry
Let's rerun the mkdir command now:
greys@vps1:~$ mkdir /tmp/try
…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:
greys@vps1:~$ ls -ald /tmp/try /tmp/oldtry drwxr-xr-x 2 greys root 4096 Nov 5 18:55 /tmp/oldtry drwxrwxr-x 2 greys greys 4096 Nov 5 19:08 /tmp/try
Remove existing file
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:
greys@vps1:~$ touch /tmp/newtry greys@vps1:~$ ls -lad /tmp/newtry -rw-rw-r-- 1 greys greys 0 Nov 5 20:50 /tmp/newtry
Now, if we try mkdir with the same name, it will fail:
greys@vps1:~$ mkdir /tmp/newtry mkdir: cannot create directory '/tmp/newtry': File exists
So, to fix the issue, we remove the file and try mkdir again:
greys@vps1:~$ rm /tmp/newtry greys@vps1:~$ mkdir /tmp/newtry
This time there were no errors, and ls command can show you that indeed you have a directory called /tmp/newtry now:
greys@vps1:~$ ls -lad /tmp/newtry drwxrwxr-x 2 greys greys 4096 Nov 5 20:50 /tmp/newtry
mkdir: cannot create directory – Permission denied
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:
greys@vps1:/tmp$ mkdir try2018 greys@vps1:/tmp$ mkdir try2018/anotherone greys@vps1:/tmp$ ls -ald try2018 drwxrwxr-x 3 greys greys 4096 Nov 5 21:04 try2018
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:
greys@vps1:/tmp$ chmod a-w try2018 greys@vps1:/tmp$ ls -ald try2018 dr-xr-xr-x 3 greys greys 4096 Nov 5 21:04 try2018
If I try creating a subdirectory now, I will get the mkdir: cannot create directory – permissions denied error:
greys@vps1:/tmp$ mkdir try2018/yetanotherone mkdir: cannot create directory 'try2018/yetanotherone': Permission denied
To fix the issue, let's add write permissions again:
greys@vps1:/tmp$ chmod a+w try2018 greys@vps1:/tmp$ mkdir try2018/yetanotherone
As you can see, try2018/yetanotherone directory was successfully created:
greys@vps1:/tmp$ ls -ald try2018/yetanotherone drwxrwxr-x 2 greys greys 4096 Nov 5 21:05 try2018/yetanotherone
That's it for today! Hope you liked this tutorial, be sure to explore more basic Unix tutorials on my blog.