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?
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?
mkdir --parents folder/subfolder/subsubfolder
mkdir -p folder/subfolder/subsubfolder
mkdir -p /dir1/dir2/dir3
Please check the manpage for details:
man mkdir
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.
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
mkdir()
so this is not a duplicate. the/commandline
BTW does not come from the OP. – Anthon May 06 '13 at 11:43/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