Linux 当目录存在时 mkdir -p 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14496897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:47:29  来源:igfitidea点击:

mkdir -p fails when directory exists

linuxshellmkdir

提问by UmNyobe

On one of our remote systems mkdir -p $directoryfails when the directory exists. which means it shows

mkdir -p $directory当目录存在时,在我们的远程系统之一上失败。这意味着它显示

mkdir: cannot create directory '$directory' : file exists

mkdir: 无法创建目录“$directory”:文件存在

This is really puzzling, as I believed the contract of -pwas that is always succeed when the directory already exists. And it works on the other systems I tried.

这真的很令人费解,因为我相信-p当目录已经存在时,is的合同总是成功的。它适用于我尝试过的其他系统。

there is a user teston all of these systems, and directory=/home/test/tmp.

test所有这些系统上都有一个用户,并且directory=/home/test/tmp.

采纳答案by Ryan Tse

This could be caused if there is already a fileby the same name located in the directory.

如果目录中已经存在同名文件,则可能会导致这种情况。

Edit: Note that a directory cannot contain both a file and folder by the same name on linux machines.

编辑:请注意,在 linux 机器上,目录不能同时包含同名的文件和文件夹。

回答by DWright

Check to see if there is a file (not a directory) with a name same as $directory.

检查是否存在与 $directory 同名的文件(不是目录)。

回答by ASHU

mkdir -p won't create directory if there is a file with the same name is existing in the same directory. Otherwise it will work as expected.

如果同一目录中存在同名文件,则 mkdir -p 不会创建目录。否则它将按预期工作。

回答by nh2

Was your directory a FUSE-based network mount by any chance?

您的目录是否是基于FUSE的网络挂载?

In addition to a file with that name already existing(other answer), this can happen when a FUSE process that once mounted something at this directory crashed(or was killed, e.g. with kill -9or via the Linux OOM killer).

除了具有该名称文件已经存在(其他答案)之外,当曾经在此目录中安装某些内容FUSE 进程崩溃(或被杀死,例如使用kill -9或通过 Linux OOM 杀手)时,可能会发生这种情况。

To see what is happening in detail, run strace -fy mkdir -p $directory, which shows all syscalls involved and their return values.

要查看详细发生的情况,请运行strace -fy mkdir -p $directory,它显示所有涉及的系统调用及其返回值。



I consider the error messages emitted in this case a bug in mkdir -p(in particular the gnuliblibrary):

我认为在这种情况下发出的错误消息是mkdir -p(特别是gnulib库)中的错误:

When you run it on a dir that had a FUSE process mounted but that process crashed, it says

当您在安装了 FUSE 进程但该进程崩溃的目录上运行它时,它说

mkdir: cannot create directory ‘/mymount': File exists

which is rather highly inaccurate, because the underlying stat()call returns ENOTCONN (Transport endpoint is not connected); but mkdirpropagates up the less-specific error from the previous mkdir()sycall. It's extra confusing because the man page says:

这是相当不准确的,因为底层stat()调用返回ENOTCONN (Transport endpoint is not connected);但从前一个sycallmkdir传播不太具体的错误。这更令人困惑,因为手册页说:mkdir()

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

so it shouldn't error if the dir exists, yet ls -l /shows:

所以如果目录存在,它不应该出错,但ls -l /显示:

d????????? ? ?    ?       ?            ? files

so according to this (d), it isa directory, but it isn't according to test -d.

所以根据这个 ( d),它一个目录,但它不是根据test -d.



I believe a better error message (which mkdir -pshould emit in this case) would be:

我相信更好的错误消息(mkdir -p在这种情况下应该发出)是:

mkdir: cannot create directory ‘/mymount': Transport endpoint is not connected