158

So this doesn't seem like a terribly complicated question I have, but it's one I can't find the answer to. I'm confused about what the -p option does in Unix. I used it for a lab assignment while creating a subdirectory and then another subdirectory within that one. It looked like this:

mkdir -p cmps012m/lab1

This is in a private directory with normal rights (rlidwka). Oh, and would someone mind giving a little explanation of what rlidwka means? I'm not a total noob to Unix, but I'm not really familiar with what this means. Hopefully that's not too vague of a question.

Grant Foster
  • 722
  • 2
  • 11
  • 21
user3476866
  • 1,589
  • 2
  • 10
  • 3
  • 3
    `man mkdir` will answer your question. As for "rlidwka", I have no idea; you'll need to give us some more context. – Keith Thompson Mar 29 '14 at 22:54
  • The `man mkdir`page found on macOS has a more intuitive description of the `-p` option (see [here](https://ss64.com/osx/mkdir.html)). Also [this answer](https://stackoverflow.com/a/21015076/814702) is useful. – informatik01 Mar 12 '23 at 03:18

5 Answers5

226

The man pages is the best source of information you can find... and is at your fingertips: man mkdir yields this about -p switch:

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

Use case example: Assume I want to create directories hello/goodbye but none exist:

$mkdir hello/goodbye
mkdir:cannot create directory 'hello/goodbye': No such file or directory
$mkdir -p hello/goodbye
$

-p created both, hello and goodbye

This means that the command will create all the directories necessaries to fulfill your request, not returning any error in case that directory exists.

About rlidwka, Google has a very good memory for acronyms :). My search returned this for example: http://www.cs.cmu.edu/~help/afs/afs_acls.html

 Directory permissions

l (lookup)
    Allows one to list the contents of a directory. It does not allow the reading of files. 
i (insert)
    Allows one to create new files in a directory or copy new files to a directory. 
d (delete)
    Allows one to remove files and sub-directories from a directory. 
a (administer)
    Allows one to change a directory's ACL. The owner of a directory can always change the ACL of a directory that s/he owns, along with the ACLs of any subdirectories in that directory. 

File permissions

r (read)
    Allows one to read the contents of file in the directory. 
w (write)
    Allows one to modify the contents of files in a directory and use chmod on them. 
k (lock)
    Allows programs to lock files in a directory. 

Hence rlidwka means: All permissions on.

It's worth mentioning, as @KeithThompson pointed out in the comments, that not all Unix systems support ACL. So probably the rlidwka concept doesn't apply here.

Grant Foster
  • 722
  • 2
  • 11
  • 21
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • 2
    But not all Unix-like systems support ACLs, so `rlidwka` may or may not be meaningful. – Keith Thompson Mar 29 '14 at 23:34
  • 1
    @KeithThompson Well I agree, I just wanted mention what the acronym stands for. – Paulo Bu Mar 29 '14 at 23:35
  • 1
    Yes, but often just answering someone's question is less helpful than providing background information needed to understand the answer. – Keith Thompson Mar 29 '14 at 23:56
  • 1
    I'll point it out in the answer. I just wasn't aware of this. Thanks for correcting me. – Paulo Bu Mar 30 '14 at 00:08
  • 15
    This answer is the most verbose way possible of saying "RTFM, let me google that for you" and I love it. I once was a noobie intern that didn't know anything, and forgot that before asking my manager I should just google it. Go easy on these new guys; sometimes it's hard to figure out what to google. You don't know what you don't know. But everyone gets more skillful at googling after years of doing it. – Dagrooms Jun 16 '17 at 14:57
  • 2
    @Dagrooms btw I came here cause I don't clearly understand the one sentence description written in the manual ! – Rémy Hosseinkhan Boucher Jun 07 '20 at 22:59
  • No info on https://linux.die.net/man/2/mkdir – KansaiRobot May 06 '23 at 06:30
9

mkdir [-switch] foldername

-p is a switch, which is optional. It will create a subfolder and a parent folder as well, even if parent folder doesn't exist.

From the man page:

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

Example:

mkdir -p storage/framework/{sessions,views,cache}

This will create subfolder sessions,views,cache inside framework folder irrespective of whether 'framework' was available earlier or not.

cheznead
  • 2,589
  • 7
  • 29
  • 50
Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
6

-p|--parent will be used if you are trying to create a directory with top-down approach. That will create the parent directory then child and so on iff none exists.

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

About rlidwka it means giving full or administrative access. Found it here https://itservices.stanford.edu/service/afs/intro/permissions/unix.

Grant Foster
  • 722
  • 2
  • 11
  • 21
Rahul
  • 76,197
  • 13
  • 71
  • 125
4

PATH: Answered long ago, however, it maybe more helpful to think of -p as "Path" (easier to remember), as in this causes mkdir to create every part of the path that isn't already there.

mkdir -p /usr/bin/comm/diff/er/fence

if /usr/bin/comm already exists, it acts like: mkdir /usr/bin/comm/diff mkdir /usr/bin/comm/diff/er mkdir /usr/bin/comm/diff/er/fence

As you can see, it saves you a bit of typing, and thinking, since you don't have to figure out what's already there and what isn't.

Able Mac
  • 846
  • 7
  • 8
2

Note that -p is an argument to the mkdir command specifically, not the whole of Unix. Every command can have whatever arguments it needs.

In this case it means "parents", meaning mkdir will create a directory and any parents that don't already exist.

Grant Foster
  • 722
  • 2
  • 11
  • 21
IMSoP
  • 89,526
  • 13
  • 117
  • 169