Can't make new dir with mkdir

121,329

Solution 1

Probably a parent directory in the path does not exist.

You can try with

mkdir -p /path-to-directory/directory-name

See man mkdir

   -p, --parents
          no error if existing, make parent directories as needed

If you get a permission denied error, you have not permissions to create a directory in the specified path.

Check if you can get around the problem by modifying the group membership or ownership, so that you get the permission needed for the whole directory path involved.

Otherwise you need elevated permissions, so try with sudo

sudo mkdir -p /path-to-directory/directory-name

Solution 2

sudodus's answer appropriately addresses how to create all directories along the given path. Alternative way would be via Python. This is especially useful if you're developing software for Ubuntu in Python and need such functionality. Calling mkdir as external command would add overhead of additional process and extra forking which would waste resources. Luckily Python's standard library, specifically os module has makedirs() function:

$ python3 -c 'import os,sys;os.makedirs(sys.argv[1])' test_1/test2/test_3
$ tree test_1
test_1
└── test2
    └── test_3

2 directories, 0 files

Note that such behavior also can be achieved in Perl, which is another scripting language that comes by default with Ubuntu.

Solution 3

I had this when the current directory literally didn't exist anymore.

I was in directory temp:

mark@mark:~/PycharmProjects/temp$ mkdir foo
mkdir: cannot create directory ‘foo’: No such file or directory

I saw the light when the current directory was empty (not even the hidden . and .. existed):

mark@mark:~/PycharmProjects/temp$ ll
total 0

One directory up temp does exist, but it is another directory with the same name. PyCharm must have deleted and recreated the project directory when I was rolling back too much changes and undoing the rollback.

mark@mark:~/PycharmProjects/temp$ cd ..
mark@mark:~/PycharmProjects$ ll
total 12
drwxrwxr-x  3 mark mark 4096 Nov  2 14:26 ./
drwxr-xr-x 40 mark mark 4096 Nov  2 14:50 ../
drwxrwxr-x  3 mark mark 4096 Nov  2 14:42 temp/
mark@mark:~/PycharmProjects$ cd temp
mark@mark:~/PycharmProjects$ mkdir foo
mark@mark:~/PycharmProjects$ 
Share:
121,329
Slobodan Vidovic
Author by

Slobodan Vidovic

Updated on September 18, 2022

Comments

  • Slobodan Vidovic
    Slobodan Vidovic almost 2 years

    When I run

    mkdir ../../bin/Release_Linux/Resources
    

    Im getting an error

    $ mkdir ../../bin/Release_Linux/Resources
    mkdir: cannot create directory ‘../../bin/Release_Linux/Resources’: No such file or directory
    

    Or just

    mkdir Release_Linux/Resources
    mkdir: cannot create directory ‘Release_Linux/Resources’: No such file or directory
    
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 5 years
    Note that elevated permission might be an overkill. If a user belongs to group which also owns the directory AND there's write permission to group set on that directory, there's no need for sudo in that case. Things can get more complex with ACL permissions, but general gist is that group ownership should remove the sudo need
  • terdon
    terdon over 5 years
    Yes, why mention sudo at all? There is no indication of a permissions issue and people abuse sudo too much already.
  • sudodus
    sudodus over 5 years
    @terdon, I mention sudo only as a second alternative, if mkdir -p does not work without it. I agree with @ Sergiy, that it might be better to use group membership/ownership in some cases, but I think there remain several cases, where sudo is necessary.
  • terdon
    terdon over 5 years
    In those cases, you get a permission denied error, which is not the case here. So since sudo isn't relevant to this case, and since using sudo when it isn't needed can be dangerous, why mention it at all?
  • sudodus
    sudodus over 5 years
    @terdon, Is it better now, that I have lowered the priority of the sudoalternative?
  • terdon
    terdon over 5 years
    Yes, thanks. It's just that I keep seeing answers suggesting sudo when it isn't needed and that causes people to use it all the time and break their system :/.
  • Melebius
    Melebius over 3 years
    Yes, this can happen. A quick way to switch to the newly created directory I use mostly is cd . however crazy it looks.