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?

Anthon
  • 78,313
  • 42
  • 165
  • 222
misteryes
  • 1,303
  • 4
  • 19
  • 24
  • 3
    @l0b0 the OP wanted the `mkdir()` so this is not a duplicate. the `/commandline` BTW does not come from the OP. – Anthon May 06 '13 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. – Jonathon Reinhart May 06 '13 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](http://unix.stackexchange.com/a/74798/7453). 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 06 '13 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 06 '13 at 12:27
  • @slm I stand embarrassingly corrected. I blame it on the lack of mobile-friendly layout for the tag edit page 0:-) – Jonathon Reinhart May 06 '13 at 12:35
  • NP, everyone makes mistakes, just pay it forward by keeping things positive next time. Everyone here is human and makes mistakes 8-). – slm May 06 '13 at 12:44
  • 1
    On SO and also for C: https://stackoverflow.com/questions/26666680/mkdir-p-equivalent-in-c – Ciro Santilli OurBigBook.com Nov 01 '19 at 12:55

4 Answers4

21
mkdir --parents folder/subfolder/subsubfolder
mkdir -p folder/subfolder/subsubfolder
Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
8
mkdir -p /dir1/dir2/dir3

Please check the manpage for details:

man mkdir
schaiba
  • 7,493
  • 1
  • 33
  • 31
  • I want to user mkdir() function in C, how to make mkdir() have -p ? – misteryes May 05 '13 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 06 '13 at 04: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.

Cœur
  • 151
  • 7
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
conny
  • 113
  • 5