mkdir's "-p" option

147,140

Solution 1

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.

Solution 2

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

Solution 3

mkdir [-switch] foldername

-p is a switch which is optional, it will create subfolder and parent folder as well even 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 'framework' was available earlier or not.

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

Solution 5

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.

Share:
147,140
user3476866
Author by

user3476866

Updated on August 23, 2022

Comments

  • user3476866
    user3476866 almost 2 years

    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.

    • Keith Thompson
      Keith Thompson over 10 years
      man mkdir will answer your question. As for "rlidwka", I have no idea; you'll need to give us some more context.
  • Keith Thompson
    Keith Thompson over 10 years
    But not all Unix-like systems support ACLs, so rlidwka may or may not be meaningful.
  • Paulo Bu
    Paulo Bu over 10 years
    @KeithThompson Well I agree, I just wanted mention what the acronym stands for.
  • Keith Thompson
    Keith Thompson over 10 years
    Yes, but often just answering someone's question is less helpful than providing background information needed to understand the answer.
  • Paulo Bu
    Paulo Bu over 10 years
    I'll point it out in the answer. I just wasn't aware of this. Thanks for correcting me.
  • Dagrooms
    Dagrooms about 7 years
    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.
  • Rémy Hosseinkhan Boucher
    Rémy Hosseinkhan Boucher about 4 years
    @Dagrooms btw I came here cause I don't clearly understand the one sentence description written in the manual !