recursive mkdirHow to create nested directory in a single command?Is it possible to make multi-level...
Ethiopian Airlines tickets seem to always have the same price regardless of the proximity of the date?
Date input format problem
How is it possible for the induced emf to take negative values in Faraday's Law of induction?
Does taxiing in London City (LCY) happen on the runway?
Bowing signs (?) in Lajos Montag's double bass method
setcounter{} is not affecting numbering
How to declare an array without specifying its size, but with an initializer inside a class in C++?
What's the physical meaning of the statement that "photons don't have positions"?
why neutral does not shock. how can a neutral be neutral in ac current?
Three Subway Escalators
Do gauntlets count as armor?
Project Euler # 25 The 1000 digit Fibonacci index
Discretisation of region intersection in 3D
Can two waves interfere head on?
I want light controlled by one switch, not two
What does "play in traffic" mean?
Could a US citizen born through "birth tourism" become President?
ESTA Travel not Authorized. Accepted twice before!
Host telling me to cancel my booking in exchange for a discount?
Has anyone ever written a novel or short story composed of only dialogue?
Inside Out and Back to Front
Is there an English word to describe when a sound "protrudes"?
Nilpotent elements of Lie algebra and unipotent groups
Why would word of Princess Leia's capture generate sympathy for the Rebellion in the Senate?
recursive mkdir
How to create nested directory in a single command?Is it possible to make multi-level directory?Single “mkdir” command to make a new directory and change to that directory at the same time?Make parent directories while creating a new fileWhat is the fastest way to create a list of directories specified in a file?How to create a file and parent directories in one command?Creating multiple directories using a parameter in a shell scriptBash can't run command from script: mkdir command not founda script to replace mkdir -p commandCreate directory name based on other other directorieshow to add a custom option to the mkdir commandIs there a programmatical difference between directories created with mktemp -d or mkdir?Single “mkdir” command to make a new directory and change to that directory at the same time?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Is there a linux command that I'm overlooking that makes it possible to do something along the lines of:
(pseudo)
$ mkdir -R foo/bar/zoo/andsoforth
Or is there no alternative but to make the directories one at a time?
mkdir
migrated from serverfault.com Sep 27 '12 at 3:56
This question came from our site for system and network administrators.
add a comment |
Is there a linux command that I'm overlooking that makes it possible to do something along the lines of:
(pseudo)
$ mkdir -R foo/bar/zoo/andsoforth
Or is there no alternative but to make the directories one at a time?
mkdir
migrated from serverfault.com Sep 27 '12 at 3:56
This question came from our site for system and network administrators.
4
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
1
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
18
Protip: In some shells you can even domkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!
– Aaron Copley
Aug 7 '12 at 20:43
1
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such sincemkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.
– jbowman
May 13 '16 at 18:03
1
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59
add a comment |
Is there a linux command that I'm overlooking that makes it possible to do something along the lines of:
(pseudo)
$ mkdir -R foo/bar/zoo/andsoforth
Or is there no alternative but to make the directories one at a time?
mkdir
Is there a linux command that I'm overlooking that makes it possible to do something along the lines of:
(pseudo)
$ mkdir -R foo/bar/zoo/andsoforth
Or is there no alternative but to make the directories one at a time?
mkdir
mkdir
edited Nov 6 '14 at 0:42
Braiam
24.4k20 gold badges82 silver badges147 bronze badges
24.4k20 gold badges82 silver badges147 bronze badges
asked Aug 7 '12 at 20:34
Dr.Dredel
migrated from serverfault.com Sep 27 '12 at 3:56
This question came from our site for system and network administrators.
migrated from serverfault.com Sep 27 '12 at 3:56
This question came from our site for system and network administrators.
4
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
1
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
18
Protip: In some shells you can even domkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!
– Aaron Copley
Aug 7 '12 at 20:43
1
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such sincemkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.
– jbowman
May 13 '16 at 18:03
1
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59
add a comment |
4
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
1
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
18
Protip: In some shells you can even domkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!
– Aaron Copley
Aug 7 '12 at 20:43
1
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such sincemkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.
– jbowman
May 13 '16 at 18:03
1
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59
4
4
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
1
1
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
18
18
Protip: In some shells you can even do
mkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!– Aaron Copley
Aug 7 '12 at 20:43
Protip: In some shells you can even do
mkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!– Aaron Copley
Aug 7 '12 at 20:43
1
1
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such since
mkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.– jbowman
May 13 '16 at 18:03
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such since
mkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.– jbowman
May 13 '16 at 18:03
1
1
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59
add a comment |
4 Answers
4
active
oldest
votes
$ mkdir -p foo/bar/zoo/andsoforth
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
Butmkdir -p
is not cross platform, doesn't work in every shell
– Роман Коптев
Apr 17 '16 at 13:40
7
@РоманКоптев,mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?
– Wildcard
Apr 18 '16 at 18:54
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
|
show 4 more comments
Using mkdir -p
is a simple way for most modern OSes:
mkdir -p foo/bar/zoo/andsoforth
However, mkdir -p
is not recommended in many manuals. Read documentation for of GNU make
and autoconf
about problems with using mkdir -p
:
- http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html#Limitations-of-Usual-Tools
- http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Utilities-in-Makefiles.html
The cross platform installation and configuration systems have their own safe alternatives for mkdir -p
.
CMake to use in shell command line:
cmake -E make_directory foo/bar/zoo/andsoforth
Autoconf to use in script with preprocessing:
AS_MKDIR_P(foo/bar/zoo/andsoforth)
or:
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)
But these solutions require cmake
or autoconf
(M4
) tools to be installed (and possible preprocessing)
You can use also install-sh
script with -d
option:
install-sh -d foo/bar/zoo/andsoforth
This script is used by autoconf
and automake
project. I think it must be the safest solution.
At the time I was searching for a cross platform solution for standard /bin/sh
without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:
#! /bin/sh
cdirname() # cross platform alternative for 'dirname'
{
# $1 - path
test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#(.)/$#1#' -e 's#^[^/]*$#.#' -e 's#(.)/[^/]*$#1#' -
}
mkd() # cross platform alternative for 'mkdir -p'
{
# $1 - directory to create
test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
test -d "$1" && return 0
test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
test -d "$1" || { mkdir "$1" || return 1; }
return 0
}
This script can be used for old systems, where option -p
for mkdir
is absent.
sed
-based cross platform version of dirname
was added to the code. It works with a way similar to dirname
(correct with path /
, paths with base name only, paths with trailing /
, paths with and without trailing n
s). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of /
(//
, ///
) with single /
Changed line mkdir "$1" || return 1
to test -d "$1" || { mkdir "$1" || return 1; }
because mkdir
terminates with error if path exists and this check is needed for paths containing constructions like aaa.
(If aaa
doesn't exist previous version creates aaa
and then tries to create it again).
This version of mkd doesn't generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
Systems that wouldn't supportmkdir -p
would be too old (and we're talking at least a couple of decades old) to have aprintf
utility or support--
to mark the end of options.
– Stéphane Chazelas
Apr 18 '16 at 14:06
2
Yourcdirname
would give the wrong answer forfoo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.
– Stéphane Chazelas
Apr 18 '16 at 14:12
2
printf
is safe on POSIX systems. POSIX systems supportmkdir -p
.mkdir -p
has been specified by POSIX for decades.--
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s.printf
first appeared as aksh
builtin I believe and was specified by POSIX later thanmkdir -p
. Some POSIX shells (like pdksh or yash based) still don't haveprintf
builtin.
– Stéphane Chazelas
Apr 18 '16 at 14:28
|
show 14 more comments
Option -p
in command mkdir
makes parent directories as needed (no error if existing):
mkdir -p foo/bar/zoo/andsoforth
Another way is, for example using &&
(error if the specified folder exists):
mkdir foo && mkdir foo/bar && mkdir foo/bar/zoo && mkdir foo/bar/zoo/andsoforth
The chaining operator &&
is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors.
Of course, the first way is better.
add a comment |
mkdir foo foo/bar foo/bar/zoo/ foo/bar/zoo/andsofort.
This should do it.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$ mkdir -p foo/bar/zoo/andsoforth
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
Butmkdir -p
is not cross platform, doesn't work in every shell
– Роман Коптев
Apr 17 '16 at 13:40
7
@РоманКоптев,mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?
– Wildcard
Apr 18 '16 at 18:54
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
|
show 4 more comments
$ mkdir -p foo/bar/zoo/andsoforth
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
Butmkdir -p
is not cross platform, doesn't work in every shell
– Роман Коптев
Apr 17 '16 at 13:40
7
@РоманКоптев,mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?
– Wildcard
Apr 18 '16 at 18:54
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
|
show 4 more comments
$ mkdir -p foo/bar/zoo/andsoforth
$ mkdir -p foo/bar/zoo/andsoforth
answered Aug 7 '12 at 20:35
EEAAEEAA
5,7101 gold badge15 silver badges12 bronze badges
5,7101 gold badge15 silver badges12 bronze badges
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
Butmkdir -p
is not cross platform, doesn't work in every shell
– Роман Коптев
Apr 17 '16 at 13:40
7
@РоманКоптев,mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?
– Wildcard
Apr 18 '16 at 18:54
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
|
show 4 more comments
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
Butmkdir -p
is not cross platform, doesn't work in every shell
– Роман Коптев
Apr 17 '16 at 13:40
7
@РоманКоптев,mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?
– Wildcard
Apr 18 '16 at 18:54
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
89
89
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
FYI the -p stands for "parents"
– carl crott
Apr 22 '15 at 17:06
12
12
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command
– Vedmant
Mar 21 '16 at 2:56
2
2
But
mkdir -p
is not cross platform, doesn't work in every shell– Роман Коптев
Apr 17 '16 at 13:40
But
mkdir -p
is not cross platform, doesn't work in every shell– Роман Коптев
Apr 17 '16 at 13:40
7
7
@РоманКоптев,
mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?– Wildcard
Apr 18 '16 at 18:54
@РоманКоптев,
mkdir -p
is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script?– Wildcard
Apr 18 '16 at 18:54
4
4
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
@StephanBijzitter Yep, this is unix.se, not unixandwindows.se. :)
– EEAA
Oct 5 '16 at 19:21
|
show 4 more comments
Using mkdir -p
is a simple way for most modern OSes:
mkdir -p foo/bar/zoo/andsoforth
However, mkdir -p
is not recommended in many manuals. Read documentation for of GNU make
and autoconf
about problems with using mkdir -p
:
- http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html#Limitations-of-Usual-Tools
- http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Utilities-in-Makefiles.html
The cross platform installation and configuration systems have their own safe alternatives for mkdir -p
.
CMake to use in shell command line:
cmake -E make_directory foo/bar/zoo/andsoforth
Autoconf to use in script with preprocessing:
AS_MKDIR_P(foo/bar/zoo/andsoforth)
or:
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)
But these solutions require cmake
or autoconf
(M4
) tools to be installed (and possible preprocessing)
You can use also install-sh
script with -d
option:
install-sh -d foo/bar/zoo/andsoforth
This script is used by autoconf
and automake
project. I think it must be the safest solution.
At the time I was searching for a cross platform solution for standard /bin/sh
without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:
#! /bin/sh
cdirname() # cross platform alternative for 'dirname'
{
# $1 - path
test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#(.)/$#1#' -e 's#^[^/]*$#.#' -e 's#(.)/[^/]*$#1#' -
}
mkd() # cross platform alternative for 'mkdir -p'
{
# $1 - directory to create
test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
test -d "$1" && return 0
test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
test -d "$1" || { mkdir "$1" || return 1; }
return 0
}
This script can be used for old systems, where option -p
for mkdir
is absent.
sed
-based cross platform version of dirname
was added to the code. It works with a way similar to dirname
(correct with path /
, paths with base name only, paths with trailing /
, paths with and without trailing n
s). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of /
(//
, ///
) with single /
Changed line mkdir "$1" || return 1
to test -d "$1" || { mkdir "$1" || return 1; }
because mkdir
terminates with error if path exists and this check is needed for paths containing constructions like aaa.
(If aaa
doesn't exist previous version creates aaa
and then tries to create it again).
This version of mkd doesn't generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
Systems that wouldn't supportmkdir -p
would be too old (and we're talking at least a couple of decades old) to have aprintf
utility or support--
to mark the end of options.
– Stéphane Chazelas
Apr 18 '16 at 14:06
2
Yourcdirname
would give the wrong answer forfoo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.
– Stéphane Chazelas
Apr 18 '16 at 14:12
2
printf
is safe on POSIX systems. POSIX systems supportmkdir -p
.mkdir -p
has been specified by POSIX for decades.--
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s.printf
first appeared as aksh
builtin I believe and was specified by POSIX later thanmkdir -p
. Some POSIX shells (like pdksh or yash based) still don't haveprintf
builtin.
– Stéphane Chazelas
Apr 18 '16 at 14:28
|
show 14 more comments
Using mkdir -p
is a simple way for most modern OSes:
mkdir -p foo/bar/zoo/andsoforth
However, mkdir -p
is not recommended in many manuals. Read documentation for of GNU make
and autoconf
about problems with using mkdir -p
:
- http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html#Limitations-of-Usual-Tools
- http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Utilities-in-Makefiles.html
The cross platform installation and configuration systems have their own safe alternatives for mkdir -p
.
CMake to use in shell command line:
cmake -E make_directory foo/bar/zoo/andsoforth
Autoconf to use in script with preprocessing:
AS_MKDIR_P(foo/bar/zoo/andsoforth)
or:
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)
But these solutions require cmake
or autoconf
(M4
) tools to be installed (and possible preprocessing)
You can use also install-sh
script with -d
option:
install-sh -d foo/bar/zoo/andsoforth
This script is used by autoconf
and automake
project. I think it must be the safest solution.
At the time I was searching for a cross platform solution for standard /bin/sh
without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:
#! /bin/sh
cdirname() # cross platform alternative for 'dirname'
{
# $1 - path
test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#(.)/$#1#' -e 's#^[^/]*$#.#' -e 's#(.)/[^/]*$#1#' -
}
mkd() # cross platform alternative for 'mkdir -p'
{
# $1 - directory to create
test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
test -d "$1" && return 0
test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
test -d "$1" || { mkdir "$1" || return 1; }
return 0
}
This script can be used for old systems, where option -p
for mkdir
is absent.
sed
-based cross platform version of dirname
was added to the code. It works with a way similar to dirname
(correct with path /
, paths with base name only, paths with trailing /
, paths with and without trailing n
s). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of /
(//
, ///
) with single /
Changed line mkdir "$1" || return 1
to test -d "$1" || { mkdir "$1" || return 1; }
because mkdir
terminates with error if path exists and this check is needed for paths containing constructions like aaa.
(If aaa
doesn't exist previous version creates aaa
and then tries to create it again).
This version of mkd doesn't generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
Systems that wouldn't supportmkdir -p
would be too old (and we're talking at least a couple of decades old) to have aprintf
utility or support--
to mark the end of options.
– Stéphane Chazelas
Apr 18 '16 at 14:06
2
Yourcdirname
would give the wrong answer forfoo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.
– Stéphane Chazelas
Apr 18 '16 at 14:12
2
printf
is safe on POSIX systems. POSIX systems supportmkdir -p
.mkdir -p
has been specified by POSIX for decades.--
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s.printf
first appeared as aksh
builtin I believe and was specified by POSIX later thanmkdir -p
. Some POSIX shells (like pdksh or yash based) still don't haveprintf
builtin.
– Stéphane Chazelas
Apr 18 '16 at 14:28
|
show 14 more comments
Using mkdir -p
is a simple way for most modern OSes:
mkdir -p foo/bar/zoo/andsoforth
However, mkdir -p
is not recommended in many manuals. Read documentation for of GNU make
and autoconf
about problems with using mkdir -p
:
- http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html#Limitations-of-Usual-Tools
- http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Utilities-in-Makefiles.html
The cross platform installation and configuration systems have their own safe alternatives for mkdir -p
.
CMake to use in shell command line:
cmake -E make_directory foo/bar/zoo/andsoforth
Autoconf to use in script with preprocessing:
AS_MKDIR_P(foo/bar/zoo/andsoforth)
or:
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)
But these solutions require cmake
or autoconf
(M4
) tools to be installed (and possible preprocessing)
You can use also install-sh
script with -d
option:
install-sh -d foo/bar/zoo/andsoforth
This script is used by autoconf
and automake
project. I think it must be the safest solution.
At the time I was searching for a cross platform solution for standard /bin/sh
without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:
#! /bin/sh
cdirname() # cross platform alternative for 'dirname'
{
# $1 - path
test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#(.)/$#1#' -e 's#^[^/]*$#.#' -e 's#(.)/[^/]*$#1#' -
}
mkd() # cross platform alternative for 'mkdir -p'
{
# $1 - directory to create
test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
test -d "$1" && return 0
test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
test -d "$1" || { mkdir "$1" || return 1; }
return 0
}
This script can be used for old systems, where option -p
for mkdir
is absent.
sed
-based cross platform version of dirname
was added to the code. It works with a way similar to dirname
(correct with path /
, paths with base name only, paths with trailing /
, paths with and without trailing n
s). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of /
(//
, ///
) with single /
Changed line mkdir "$1" || return 1
to test -d "$1" || { mkdir "$1" || return 1; }
because mkdir
terminates with error if path exists and this check is needed for paths containing constructions like aaa.
(If aaa
doesn't exist previous version creates aaa
and then tries to create it again).
This version of mkd doesn't generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.
Using mkdir -p
is a simple way for most modern OSes:
mkdir -p foo/bar/zoo/andsoforth
However, mkdir -p
is not recommended in many manuals. Read documentation for of GNU make
and autoconf
about problems with using mkdir -p
:
- http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html#Limitations-of-Usual-Tools
- http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Utilities-in-Makefiles.html
The cross platform installation and configuration systems have their own safe alternatives for mkdir -p
.
CMake to use in shell command line:
cmake -E make_directory foo/bar/zoo/andsoforth
Autoconf to use in script with preprocessing:
AS_MKDIR_P(foo/bar/zoo/andsoforth)
or:
AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)
But these solutions require cmake
or autoconf
(M4
) tools to be installed (and possible preprocessing)
You can use also install-sh
script with -d
option:
install-sh -d foo/bar/zoo/andsoforth
This script is used by autoconf
and automake
project. I think it must be the safest solution.
At the time I was searching for a cross platform solution for standard /bin/sh
without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:
#! /bin/sh
cdirname() # cross platform alternative for 'dirname'
{
# $1 - path
test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
echo "$1" | sed -n -e '1p' | sed -e 's#//*#/#g' -e 's#(.)/$#1#' -e 's#^[^/]*$#.#' -e 's#(.)/[^/]*$#1#' -
}
mkd() # cross platform alternative for 'mkdir -p'
{
# $1 - directory to create
test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
test -d "$1" && return 0
test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
test -d "$1" || { mkdir "$1" || return 1; }
return 0
}
This script can be used for old systems, where option -p
for mkdir
is absent.
sed
-based cross platform version of dirname
was added to the code. It works with a way similar to dirname
(correct with path /
, paths with base name only, paths with trailing /
, paths with and without trailing n
s). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of /
(//
, ///
) with single /
Changed line mkdir "$1" || return 1
to test -d "$1" || { mkdir "$1" || return 1; }
because mkdir
terminates with error if path exists and this check is needed for paths containing constructions like aaa.
(If aaa
doesn't exist previous version creates aaa
and then tries to create it again).
This version of mkd doesn't generate an error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.
edited 28 mins ago
muru
42.2k5 gold badges103 silver badges177 bronze badges
42.2k5 gold badges103 silver badges177 bronze badges
answered Apr 17 '16 at 22:45
Роман КоптевРоман Коптев
2792 silver badges5 bronze badges
2792 silver badges5 bronze badges
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
Systems that wouldn't supportmkdir -p
would be too old (and we're talking at least a couple of decades old) to have aprintf
utility or support--
to mark the end of options.
– Stéphane Chazelas
Apr 18 '16 at 14:06
2
Yourcdirname
would give the wrong answer forfoo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.
– Stéphane Chazelas
Apr 18 '16 at 14:12
2
printf
is safe on POSIX systems. POSIX systems supportmkdir -p
.mkdir -p
has been specified by POSIX for decades.--
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s.printf
first appeared as aksh
builtin I believe and was specified by POSIX later thanmkdir -p
. Some POSIX shells (like pdksh or yash based) still don't haveprintf
builtin.
– Stéphane Chazelas
Apr 18 '16 at 14:28
|
show 14 more comments
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
Systems that wouldn't supportmkdir -p
would be too old (and we're talking at least a couple of decades old) to have aprintf
utility or support--
to mark the end of options.
– Stéphane Chazelas
Apr 18 '16 at 14:06
2
Yourcdirname
would give the wrong answer forfoo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.
– Stéphane Chazelas
Apr 18 '16 at 14:12
2
printf
is safe on POSIX systems. POSIX systems supportmkdir -p
.mkdir -p
has been specified by POSIX for decades.--
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s.printf
first appeared as aksh
builtin I believe and was specified by POSIX later thanmkdir -p
. Some POSIX shells (like pdksh or yash based) still don't haveprintf
builtin.
– Stéphane Chazelas
Apr 18 '16 at 14:28
4
4
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
– Tim B
Apr 17 '16 at 22:49
4
4
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
Some of the information in that page is only 15 years old. Some is older.
– Thomas Dickey
Apr 17 '16 at 23:30
7
7
Systems that wouldn't support
mkdir -p
would be too old (and we're talking at least a couple of decades old) to have a printf
utility or support --
to mark the end of options.– Stéphane Chazelas
Apr 18 '16 at 14:06
Systems that wouldn't support
mkdir -p
would be too old (and we're talking at least a couple of decades old) to have a printf
utility or support --
to mark the end of options.– Stéphane Chazelas
Apr 18 '16 at 14:06
2
2
Your
cdirname
would give the wrong answer for foo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.– Stéphane Chazelas
Apr 18 '16 at 14:12
Your
cdirname
would give the wrong answer for foo/bar//
, or directory names containing newline characters, or invalid characters in the current locale.– Stéphane Chazelas
Apr 18 '16 at 14:12
2
2
printf
is safe on POSIX systems. POSIX systems support mkdir -p
. mkdir -p
has been specified by POSIX for decades. --
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s. printf
first appeared as a ksh
builtin I believe and was specified by POSIX later than mkdir -p
. Some POSIX shells (like pdksh or yash based) still don't have printf
builtin.– Stéphane Chazelas
Apr 18 '16 at 14:28
printf
is safe on POSIX systems. POSIX systems support mkdir -p
. mkdir -p
has been specified by POSIX for decades. --
started to be widespread on AT&T systems starting with SysIII in the early-mid-80s. printf
first appeared as a ksh
builtin I believe and was specified by POSIX later than mkdir -p
. Some POSIX shells (like pdksh or yash based) still don't have printf
builtin.– Stéphane Chazelas
Apr 18 '16 at 14:28
|
show 14 more comments
Option -p
in command mkdir
makes parent directories as needed (no error if existing):
mkdir -p foo/bar/zoo/andsoforth
Another way is, for example using &&
(error if the specified folder exists):
mkdir foo && mkdir foo/bar && mkdir foo/bar/zoo && mkdir foo/bar/zoo/andsoforth
The chaining operator &&
is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors.
Of course, the first way is better.
add a comment |
Option -p
in command mkdir
makes parent directories as needed (no error if existing):
mkdir -p foo/bar/zoo/andsoforth
Another way is, for example using &&
(error if the specified folder exists):
mkdir foo && mkdir foo/bar && mkdir foo/bar/zoo && mkdir foo/bar/zoo/andsoforth
The chaining operator &&
is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors.
Of course, the first way is better.
add a comment |
Option -p
in command mkdir
makes parent directories as needed (no error if existing):
mkdir -p foo/bar/zoo/andsoforth
Another way is, for example using &&
(error if the specified folder exists):
mkdir foo && mkdir foo/bar && mkdir foo/bar/zoo && mkdir foo/bar/zoo/andsoforth
The chaining operator &&
is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors.
Of course, the first way is better.
Option -p
in command mkdir
makes parent directories as needed (no error if existing):
mkdir -p foo/bar/zoo/andsoforth
Another way is, for example using &&
(error if the specified folder exists):
mkdir foo && mkdir foo/bar && mkdir foo/bar/zoo && mkdir foo/bar/zoo/andsoforth
The chaining operator &&
is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors.
Of course, the first way is better.
answered Apr 18 '18 at 20:32
simhumilecosimhumileco
2392 silver badges10 bronze badges
2392 silver badges10 bronze badges
add a comment |
add a comment |
mkdir foo foo/bar foo/bar/zoo/ foo/bar/zoo/andsofort.
This should do it.
add a comment |
mkdir foo foo/bar foo/bar/zoo/ foo/bar/zoo/andsofort.
This should do it.
add a comment |
mkdir foo foo/bar foo/bar/zoo/ foo/bar/zoo/andsofort.
This should do it.
mkdir foo foo/bar foo/bar/zoo/ foo/bar/zoo/andsofort.
This should do it.
edited Dec 20 '17 at 8:19
Kevdog777
2,12713 gold badges35 silver badges61 bronze badges
2,12713 gold badges35 silver badges61 bronze badges
answered Dec 20 '17 at 7:00
Kanchan KumarKanchan Kumar
1
1
add a comment |
add a comment |
4
linux.die.net/man/1/mkdir
– Zoredache
Aug 7 '12 at 20:36
1
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right).
– Dr.Dredel
Aug 7 '12 at 20:37
18
Protip: In some shells you can even do
mkdir -p foo/{bar,baz}/zoo/andsoforth
. Very useful!– Aaron Copley
Aug 7 '12 at 20:43
1
Personally, I overlooked "parent" in the man page as well because, well, I feel like the flag should be "-r" for "recursive" - or at the very least, there should be an alias for such since
mkdir
has only 6 documented flags in total. Hell, I feel like the command should create directories recursively by default, and if you don't want this then you should have to specify so explicitly.– jbowman
May 13 '16 at 18:03
1
stackoverflow.com/questions/1731767/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 16 '16 at 16:59