- Ошибка: mkdir — Cannot Create Directory
- mkdir: cannot create directory – File exists
- Возможные решения проблем mkdir: cannot create directory
- Сценарий file exists
- Переименовать (или переместить) существующий файл
- Удалить существующий файл
- mkdir: cannot create directory ‘/usr/local/n/versions’: Permission denied [closed]
- 1 Answer 1
- Not the answer you’re looking for? Browse other questions tagged linux node.js ubuntu or ask your own question.
- Related
- Hot Network Questions
- Why isn’t mkdir -p working right in a script called by checkinstall?
- How to mkdir only if a directory does not already exist?
- 16 Answers 16
- Linux: copy and create destination dir if it does not exist
- 23 Answers 23
- Short Answer
- How does this work?
Ошибка: mkdir — Cannot Create Directory
Новички в Linux часто не понимают, что делать при получении ошибки “mkdir: cannot create directory” во время работы с командной строкой. Есть несколько причин возникновения такой ошибки, и в этом переводе своей англоязычной статьи с сайта Unix Tutorial я покажу эти причины и их устрание на примерах.
mkdir: cannot create directory – File exists
В переводе с английского сообщение означает: невозможно создать каталог — файл уже существует.
ФАЙЛ существует? А при чём тут проблема создания каталога? И почему ошибка говорить “существует файл”, когда мы вообще пытаемся создавать каталог, а не файл?
На самом деле всё просто: большинство объектов в Linux являются файлами и структурами в файловой системе. Поэтому эта ошибка означает, что там, где вы пытаетесь выполнить команду создания нового каталога, уже существует другой объект с таким же именем. В данном случае — это файл, а не каталог. Но у файла такое же имя, как у желаемого каталога, так что создать второй объект с таким же именем не получится.
намекает, что у нас уже есть файл с именем /tmp/try.
Очень просто проверить эту гипотезу с помощью команды ls:
Так и есть, у нас существует файл с таким именем.
Возможные решения проблем mkdir: cannot create directory
Сценарий file exists
Если файл с таким именем уже существует, а каталог всё же очень хочется создать, то есть решения.
Переименовать (или переместить) существующий файл
Используем команду mv для перемещения /tmp/try в другой каталог (или просто сменим имя try на другое, оставив файл в том же каталоге /tmp). Вот как можно переименовать файл в имя oldtry:
Теперь давайте попробуем ту же команду mkdir:
…и всё замечательно работает! Никаких ошибок, и создался новый каталог под названием /tmp/try. Подтверждаем это с помощью команды ls:
Удалить существующий файл
Ещё одна опция, которая напрашивается сама собой — можно просто удалить неугодный файл, который мешает созаднию нашего нового каталога.
Для этого примера создадим новый пустой файл с названием /tmp/newtry
Если попробовать mkdir, то получится ожидаемая ошибка:
А теперь мы просто удалим неугодный файл и попробуем mkdir снова:
В этот раз нет никаких ошибок, всё снова сработало:
##mkdir: cannot create directory – Permission denied
Это — ещё один распространённый сценарий при создании каталогов.
В переводе на русский, сообщение говорит: невозможно создать каталог — недостаточно прав доступа. То есть файлов с таким же именем нет, но текущий пользователь, под которым мы пытаемся создать каталог, не имеет прав в текущем месте файловой системы для создания новых каталогов (и файлов).
Основной подход к такой ошибке — проверка прав доступа в каталоге, где получена ошибка. Команда ls и здесь поможет. You should use ls command on the higher level directory to confirm permissions.
Все эти команды сработали без ошибок, и ls показывает, что у меня есть полные права доступа к каталогу try2018 — rwx для меня, rwx для моей группы и r-x для всех остальных (это я читаю фрагмент drwxrwxr-x в строке с try2018).
Теперь давайте уберём права на запись (и создание новых объектов) в каталоге try2018:
Теперь мои права к этому каталогу сменились с полных (rwx — read/write/execute) на только чтение (r-x — read/execute). Так что если я попробую создать в try2018 какой-то подкаталог, выйдет та самая ошибка про недостаток прав доступа:
Чтобы исправить проблему, нужно исправить права доступа на каталоге, где мы видим ошибку. И пробуем mkdir снова:
Вот теперь — порядок, всё создалось,
На сегодня — всё! Будут ещё вопросы по самым основам Linux — обращайтесь!
Источник
mkdir: cannot create directory ‘/usr/local/n/versions’: Permission denied [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 6 years ago .
When I run npm install n -g on my computer (Ubuntu), I have these errors:
1 Answer 1
Are you logged in as root user on your ubuntu machine? If not, try running the commands with sudo .
You also may have to chown the /usr/local/n directory to your user.
It’s often one of these that cause the permission denied error you’re seeing.
n 4.1.0 install : node-v4.1.0 mkdir : /usr/local/n/versions/node/4.1.0 mkdir: cannot create directory ‘/usr/local/n/versions/node/4.1.0’: Permission denied Error: sudo required` ➜
sudo n 4.1.0 sudo: unable to resolve host root [sudo] password for joes: sudo: n: command not found
Not the answer you’re looking for? Browse other questions tagged linux node.js ubuntu or ask your own question.
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Why isn’t mkdir -p working right in a script called by checkinstall?
I’m trying to compile Quarter and package it using checkinstall.
If I do the standard ./configure && make && sudo make install, things go fine.
But when I use checkinstall, it fails on a mkdir -p that should work perfectly fine. The way it fails is exactly how it would as if the -p option weren’t given. This is the checkinstall command line I’m using:
This is the failure:
This is the relevant part of the script:
I don’t understand why that exec is there — doesn’t that guarantee that the remainder of the script (after the esac ) will never execute? (If the if test passes, then the script assumes mkdir -p works correctly, so once it does the real mkdir -p it can quit; otherwise the remainder of the script implements proper mkdir -p behavior.) I also don’t understand why it uses «$*» in the echo and «$@» in the next line, but it doesn’t seem to matter — they’re both the same thing since this script is being called with just one argument. (Tom explained in comment.)
If I add two lines between echo and exec that does mkdir -p — «$@» and then echo «Now doing the exec mkdir. » then it works like this — better, but still bewildering:
Now, the fact that doing the mkdir line twice made it work tells me it’s not a permissions issue (beside, that would generate a different diagnostic from mkdir, and this is being run as sudo , and it’s actually working in /var/tmp/. not the real /usr/local/. ). I think what’s happening is that the first mkdir invocation (the one I added) is actually creating just the Quarter directory and bailing out, and then when the second mkdir runs, it’s able to create the devices subdirectory, because the Quarter directory is already there. But why would mkdir work that way.
My workaround is to patch that mkinstalldirs script somehow, but I’m really curious why this is breaking!
This is a Ubuntu 10.10 guest running in VirtualBox on Win7, checkinstall version 1.6.2 installed thru apt-get.
EDIT: I did some testing to see what works and what fails in this environment.
Weird that -p works for relative pathnames but not for absolute pathnames. Or maybe the correct distinction is that -p works outside of the «chroot» tree (if it’s even really using chroot) but not within it.
I also verified that despite the failure, it is able to create the first directory level.
Источник
How to mkdir only if a directory does not already exist?
I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the «File exists» error that mkdir throws when it tries to create an existing directory.
How can I best do this?
16 Answers 16
Note that this will also create any intermediate directories that don’t exist; for instance,
will create directories foo , foo/bar , and foo/bar/baz if they don’t exist.
Some implementation like GNU mkdir include mkdir —parents as a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial Unixes, so it should be avoided.
If you want an error when parent directories don’t exist, and want to create the directory if it doesn’t exist, then you can test for the existence of the directory first:
This should work:
which will create the directory if it doesn’t exist, but warn you if the name of the directory you’re trying to create is already in use by something other than a directory.
Defining complex directory trees with one command
If you don’t want to show any error message:
If you want to show your own error message:
The old tried and true
will do what you want with none of the race conditions many of the other solutions have.
Sometimes the simplest (and ugliest) solutions are the best.
mkdir foo works even if the directory exists. To make it work only if the directory named «foo» does not exist, try using the -p flag.
This will create the directory named «foo» only if it does not exist. 🙂
mkdir does not support -p switch anymore on Windows 8+ systems.
You can use this:
You can either use an if statement to check if the directory exists or not. If it does not exits, then create the directory.
You can directory use mkdir with -p option to create a directory. It will check if the directory is not available it will.
mkdir -p also allows to create the tree structure of the directory. If you want to create the parent and child directories using same command, can opt mkdir -p
Referring to man page man mkdir for option — p
which will create all directories in a given path, if exists throws no error otherwise it creates all directories from left to right in the given path. Try the below command. the directories newdir and anotherdir doesn’t exists before issuing this command
Correct Usage
mkdir -p /tmp/newdir/anotherdir
After executing the command you can see newdir and anotherdir created under /tmp. You can issue this command as many times you want, the command always have exit(0) . Due to this reason most people use this command in shell scripts before using those actual paths.
Источник
Linux: copy and create destination dir if it does not exist
I want a command (or probably an option to cp) that creates the destination directory if it does not exist.
23 Answers 23
(there’s no such option for cp ).
If both of the following are true:
- You are using the GNU version of cp (and not, for instance, the Mac version), and
- You are copying from some existing directory structure and you just need it recreated
then you can do this with the —parents flag of cp . From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or man cp ):
Short Answer
To copy myfile.txt to /foo/bar/myfile.txt , use:
How does this work?
There’s a few components to this, so I’ll cover all the syntax step by step.
The mkdir utility, as specified in the POSIX standard, makes directories. The -p argument, per the docs, will cause mkdir to
Create any missing intermediate pathname components
meaning that when calling mkdir -p /foo/bar , mkdir will create /foo and /foo/bar if /foo doesn’t already exist. (Without -p , it will instead throw an error.
The && list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. This means the cp command won’t try to execute if mkdir fails for one of the many reasons it might fail.
Finally, the $_ we pass as the second argument to cp is a «special parameter» which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable. Per the Bash manual, it:
expands to the last argument to the previous command
In this case, that’s the /foo/bar we passed to mkdir . So the cp command expands to cp myfile.txt /foo/bar , which copies myfile.txt into the newly created /foo/bar directory.
Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn’t support this construct. However, I don’t know of any modern shells that don’t support $_ ; certainly Bash, Dash, and zsh all do.
A final note: the command I’ve given at the start of this answer assumes that your directory names don’t have spaces in. If you’re dealing with names with spaces, you’ll need to quote them so that the different words aren’t treated as different arguments to mkdir or cp . So your command would actually look like:
Such an old question, but maybe I can propose an alternative solution.
You can use the install programme to copy your file and create the destination path «on the fly».
There are some aspects to take in consideration, though:
- you need to specify also the destination file name, not only the destination path
- the destination file will be executable (at least, as far as I saw from my tests)
You can easily amend the #2 by adding the -m option to set permissions on the destination file (example: -m 664 will create the destination file with permissions rw-rw-r— , just like creating a new file with touch ).
Shell function that does what you want, calling it a «bury» copy because it digs a hole for the file to live in:
Here’s one way to do it:
dirname will give you the parent of the destination directory or file. mkdir -p `dirname . ` will then create that directory ensuring that when you call cp -r the correct base directory is in place.
The advantage of this over —parents is that it works for the case where the last element in the destination path is a filename.
And it’ll work on OS X.
install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there
with all my respect for answers above, I prefer to use rsync as follow:
Just to resume and give a complete working solution, in one line. Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir. Next code should work in any case.
or bash specific
This does it for me
Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.
E.g If you want to copy ‘test’ file to destination directory ‘d’ Use,
mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.
I wrote a support script for cp, called CP (note capital letters) that’s intended to do exactly this. Script will check for errors in the path you’ve put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.
- all the info here can be accessed by doing CP —help. CP —help-all include’s cp’s switches.
- regular cp won’t do the copy if it doesn’t find the destination path. You don’t have such a safety net for typos with CP. You’re destination will be created, so if you misspell your destination as /usrr/share/icons or /usr/share/icon well that’s what’s going to be created.
- regular cp tends to model it’s behavior on the existing path: cp /a/b /c/d will vary on whether d exists or not. if d is an existing folder, cp will copy b into it, making /c/d/b. If d doesn’t exist, b will be copied into c and renamed to d. If d exists but is a file and b is a file, it will be overwritten by b’s copy. If c doesn’t exist, cp doesn’t do the copy and exits.
CP doesn’t have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. CP assumes that the item you’re copying is being dropped in the destination path and is not the destination itself (aka, a renamed copy of the source file/folder). Meaning:
- «CP /a/b /c/d» will result in /c/d/b if d is a folder
- «CP /a/b /c/b» will result in /c/b/b if b in /c/b is a folder.
- If both b and d are files: CP /a/b /c/d will result in /c/d (where d is a copy of b). Same for CP /a/b /c/b in the same circumstance.
This default CP behavior can be changed with the «—rename» switch. In this case, it’s assumed that
- «CP —rename /a/b /c/d» is copying b into /c and renaming the copy to d.
A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.
CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through —strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (—relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.
Источник