当目录存在时,为什么 mkdir -p /tmp/one/two/tree 不打印任何内容?

当目录存在时,为什么 mkdir -p /tmp/one/two/tree 不打印任何内容?

当我mkdir -p在现有目录上运行时,它不会打印任何内容,

strace -e mkdir,write mkdir -p /tmp/one/two/tree
mkdir("/tmp", 0777)                     = -1 EEXIST (File exists)
mkdir("one", 0777)                      = -1 EEXIST (File exists)
mkdir("two", 0777)                      = -1 EEXIST (File exists)
mkdir("tree", 0777)                     = -1 EEXIST (File exists)
+++ exited with 0 +++

但是当我mkdir不带-p选项运行时,它会通知我该目录存在。

strace -e mkdir,write mkdir  /tmp/one/two/tree
mkdir("/tmp/one/two/tree", 0777)        = -1 EEXIST (File exists)
write(2, "mkdir: ", 7mkdir: )                  = 7
write(2, "cannot create directory \342\200\230/tmp/"..., 47cannot create directory ‘/tmp/one/two/tree’) = 47
write(2, ": File exists", 13: File exists)           = 13
write(2, "\n", 1
)        

           = 1

答案1

因为这就是该标志应该做的事情;这来自man mkdir(来自 GNU coreutils 8.21):

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

相关内容