8

Using mkdir() (the C function) I can create a 1-level directory, if I want to create multi_level directory like:

folder/subfolder/subsubfolder

is it possible? if so, how?

7
  • 3
    @l0b0 the OP wanted the mkdir() so this is not a duplicate. the /commandline BTW does not come from the OP.
    – Anthon
    May 6, 2013 at 11:43
  • 2
    If you were looking for the C code to do this, a) you should have mentioned it before everyone answered, and b) asked it on Stackoverflow. May 6, 2013 at 12:03
  • @JonathonReinhart, not understanding your comment to me? I added the /c tag based on others comments above and also the OP left a comment that he wanted mkdir() on @schaiba's answer. The rest of the edits were others, I was the last to add the tag! Please read the edit history before jumping to conclusions!!!
    – slm
    May 6, 2013 at 12:21
  • @JonathonReinhart I propose you look at the history for the edits, Gilles was the one adding the /command-line tag. @slm is 'just' the last one to add a revision to the this question (as of now)
    – Anthon
    May 6, 2013 at 12:27
  • 1

4 Answers 4

22
mkdir --parents folder/subfolder/subsubfolder
mkdir -p folder/subfolder/subsubfolder
7
  • I want to user mkdir() function in C, how to make mkdir() have -p ?
    – misteryes
    May 5, 2013 at 22:13
  • @misteryes Look at the mkdir.c source code in GNU coreutils?
    – jordanm
    May 5, 2013 at 22:14
  • 2
    (mkdir -p is standard. --parents is GNU, Not Unix.) May 6, 2013 at 10:10
  • 2
    Why was this marked as an answer if the question is about C code?
    – l0b0
    May 6, 2013 at 19:40
  • 1
    @l0b0 That has surprised me, too. And 18 upvotes for this trivial answer whereas my brilliant answers elsewhere get ignored... And now it's even closed as a duplicate though it's absolutely obvious that both questions are seriously different. This is certainly no question for SE to be proud of. :-) May 6, 2013 at 20:08
8
mkdir -p /dir1/dir2/dir3

Please check the manpage for details:

man mkdir
2
  • I want to user mkdir() function in C, how to make mkdir() have -p ?
    – misteryes
    May 5, 2013 at 22:15
  • @misteryes You have to write an algorithm in terms of processing the path as a string, and making multiple system calls.
    – Kaz
    May 6, 2013 at 4:22
5

Something along the lines of:

#include <libgen.h>

// safe
void mkdir_recursive(const char *path)
{
    char *subpath, *fullpath;
    
    fullpath = strdup(path);
    subpath = dirname(fullpath);
    if (strlen(subpath) > 1)
        mkdir_recursive(subpath);
    mkdir(path);
    free(fullpath);
}

or:

#include <string.h>

// only pass a path starting with a trailing slash
// (if path starts with a dot, it will loop and crash)
void mkdir_recursive(const char *path)
{
    char *subpath, *fullpath;
    
    fullpath = strdup(path);
    subpath = basename(fullpath);
    if (strlen(subpath) > 0)
        mkdir_recursive(subpath);
    mkdir(path);
    free(fullpath);
}

The first way should always works. The second way should only work if your path starts with a trailing slash, because it will loop on paths starting with a dot.

0
0

In case -p is not available, argument lists are typically parsed in the order they appear on the command line, thus:

mkdir adam adam/bertil adam/bertil/caesar

is functionally equivalent to

mkdir -p adam/bertil/caesar

Not the answer you're looking for? Browse other questions tagged .