【问题标题】:mkdir -p returns error " file exists"mkdir -p 返回错误“文件存在”
【发布时间】:2013-06-24 20:49:00
【问题描述】:

我在 AIX 上尝试了mkdir -p /a/b/c。当ab 不存在时,此命令创建abc。但是当ab都存在时,会报错

无法创建 /a/b。 /a/b: 文件存在

并返回错误代码 2。

有什么帮助吗?

【问题讨论】:

标签: shell aix


【解决方案1】:

我想你说的是这个场景:

bash-2.02# mkdir -p /a/c/d
bash-2.02# rm -rf /a/c/d
bash-2.02# mkdir -p /a/c/d
mkdir: cannot create /a/c
/a/c: File exists
bash-2.02# echo $?
2
bash-2.02#

【讨论】:

  • 是的,即使“a”和“b”是目录,它说“文件存在”,我运行命令“mkdir -p /a/b/c”,仍然错误说“ /a/b 存在”。没有同名文件。我认为 mkdir -p 应该在路径中创建每个目录(如果没有同名的文件)
  • @vik123 这如何回答这个问题?
【解决方案2】:

我刚刚遇到了类似的症状 - 除了它是一个损坏的远程挂载点(在这种情况下使用 sshfs)并且与 file 被“阻碍”无关: p>

$ mkdir -p /mnt/sshfs-remote
mkdir: cannot create directory `/mnt/sshfs-remote': File exists
$ ls -lscrath /mnt/sshfs-remote
/bin/ls: cannot access /mnt/sshfs-remote: No such file or directory
$ ls -lscrath /mnt
/bin/ls: cannot access /mnt/sshfs-remote: No such file or directory
total 4.0K
    ? d?????????  ? ?    ?        ?            ? sshfs-remote/

一个 umount* 把它整理出来。我还在脚本中添加了一个异常,该异常触发了错误,也可以尝试 umount。

$ umount -l /mnt/sshfs-remote ; mount /mnt/sshfs-remote
$ ls -lsahd /mnt/sshfs-remote
4.0K drwxr-xr-x 1 root root 6 Mar 11 09:20 /mnt/sshfs-remote/
$ mkdir -p /mnt/sshfs-remote
$ echo $?
0

*如果有人想知道我在 umount 上使用的 -l:这可能是不必要的 - 但在远程安装上,我发现它是一种更清洁/更简单的方式来“继续使用它”。从 umount 手册页:

   -l, --lazy
          Lazy unmount.  Detach the filesystem from the file hierarchy now, and clean up all references to this filesystem as soon
          as it is not busy anymore.  (Requires kernel 2.4.11 or later.)

【讨论】:

  • 这行得通,对我来说,我不得不使用umount -f "full path of the dir"。很奇怪,执行ls时,它甚至不会显示挂载的文件夹。
【解决方案3】:

我在使用 Parallels 文件系统时遇到了这种情况。虚拟机抱怨存在一个目录,即使“ls”看不到它。当我进入目录时,它允许它,但随后 ls 会失败。所以这似乎是一个文件系统缓存错误。我通过转到主机并使用文件创建目录,然后返回虚拟机并删除目录来解决它。之后,虚拟机文件系统正确同步,我可以正常使用 mkdir 创建目录。

在虚拟机上:

> mkdir -p build/a/b/c  <-- failed with "file exists"
> cd build              <-- allowed
> ls                    <-- failed

在主机上:

> mkdir build
> touch build/foo

在虚拟机上:

> rm -rf build
> mkdir -p build/a/b/c    <-- Success

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2013-01-07
    相关资源
    最近更新 更多