0
mkdir -p /path/to/something

when does the above command exit with nonzero exit status?

Does it happen, if and only if /path/to/something doesn't exist as any file (or as a directory?) before running the command and the command fails to create it as a directory?

man mkdir says

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

Thanks.

1
  • Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value. – cylgalad May 8 '18 at 7:25
6

To elaborate on the previous answer with a few examples.
It could be when trying to create folders inside system folders:

ubuntu:~$ mkdir /proc/test
mkdir: cannot create directory ‘/proc/test’: No such file or directory
ubuntu:~$ mkdir -p /dev/null/test
mkdir: cannot create directory ‘/dev/null’: Not a directory

Or when you don't have permission to the previous folder:

ubuntu:~$ mkdir -p /root/test
mkdir: cannot create directory ‘/root’: Permission denied

It will also fail if there is a file by the same name in the folder:

ubuntu:~$ touch /tmp/test
ubuntu:~$ mkdir -p /tmp/test
mkdir: cannot create directory ‘/tmp/test’: File exists
2

From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:

An exit status of zero indicates success, and a nonzero value indicates failure. 

Failure is when the directories need to be created but the operation fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.