1
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.

Newtonx
  • 103
Tim
  • 101,790
  • 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 08 '18 at 07:25

2 Answers2

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
Mikael Kjær
  • 1,001
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.