How to use Bash to create a folder if it doesn't already exist?
#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
This doesn't seem to work. Can anyone help?
bash folder
add a comment |
#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
This doesn't seem to work. Can anyone help?
bash folder
3
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
1
The;token is a command separator, so is newline. Asthenis a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons aftermkdirandfiare superflous.
– Andreas Riedmüller
Jun 27 '18 at 7:26
add a comment |
#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
This doesn't seem to work. Can anyone help?
bash folder
#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
This doesn't seem to work. Can anyone help?
bash folder
bash folder
edited Feb 5 '11 at 11:27
templatetypedef
267k69679897
267k69679897
asked Feb 5 '11 at 11:25
mlzboymlzboy
5,811236591
5,811236591
3
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
1
The;token is a command separator, so is newline. Asthenis a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons aftermkdirandfiare superflous.
– Andreas Riedmüller
Jun 27 '18 at 7:26
add a comment |
3
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
1
The;token is a command separator, so is newline. Asthenis a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons aftermkdirandfiare superflous.
– Andreas Riedmüller
Jun 27 '18 at 7:26
3
3
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
1
1
The
; token is a command separator, so is newline. As then is a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons after mkdir and fi are superflous.– Andreas Riedmüller
Jun 27 '18 at 7:26
The
; token is a command separator, so is newline. As then is a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons after mkdir and fi are superflous.– Andreas Riedmüller
Jun 27 '18 at 7:26
add a comment |
6 Answers
6
active
oldest
votes
First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and thats it.
1
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
add a comment |
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
2
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
add a comment |
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
2
The argument-pdoesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.
– BeeOnRope
Nov 15 '18 at 23:26
add a comment |
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
add a comment |
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the-pargument makes the check unnecessary. You can still use it when you don't want to use-p, that is when you don't want all the parent directories to be created automatically.
– ADTC
Apr 13 '16 at 11:11
2
Actually it's even shorter to write[ -d /path/to/dir ] || mkdir /path/to/dir.. right side is executed when the left side is false.
– ADTC
Apr 13 '16 at 11:23
add a comment |
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4906579%2fhow-to-use-bash-to-create-a-folder-if-it-doesnt-already-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and thats it.
1
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
add a comment |
First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and thats it.
1
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
add a comment |
First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and thats it.
First, in bash "[" is just a command, which expects string "]" as a last argument, so the whitespace before the closing bracket (as well as between "!" and "-d" which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch to mkdir, this check is useless, because this is what does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and thats it.
edited Aug 25 '16 at 4:17
pcambra
240111
240111
answered Feb 5 '11 at 11:48
Maxim SloykoMaxim Sloyko
9,56543045
9,56543045
1
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
add a comment |
1
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
1
1
Note: the
-p flag causes any parent directories to be created if necessary.– Danijel
May 15 '18 at 8:03
Note: the
-p flag causes any parent directories to be created if necessary.– Danijel
May 15 '18 at 8:03
4
4
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
My god, I've never knew that "[" is a command. This explains so many of my problems... Easily the most useful thing I've ever read on StackOverflow.
– Ben Kushigian
Nov 16 '18 at 2:11
add a comment |
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
2
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
add a comment |
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
2
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
add a comment |
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
answered Feb 5 '11 at 12:00
kurumikurumi
20.1k33344
20.1k33344
2
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
add a comment |
2
Note: the-pflag causes any parent directories to be created if necessary.
– Danijel
May 15 '18 at 8:03
2
2
Note: the
-p flag causes any parent directories to be created if necessary.– Danijel
May 15 '18 at 8:03
Note: the
-p flag causes any parent directories to be created if necessary.– Danijel
May 15 '18 at 8:03
add a comment |
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
2
The argument-pdoesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.
– BeeOnRope
Nov 15 '18 at 23:26
add a comment |
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
2
The argument-pdoesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.
– BeeOnRope
Nov 15 '18 at 23:26
add a comment |
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
edited Nov 16 '18 at 10:49
answered Feb 25 '13 at 13:56
AutomaticoAutomatico
6,78324888
6,78324888
2
The argument-pdoesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.
– BeeOnRope
Nov 15 '18 at 23:26
add a comment |
2
The argument-pdoesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.
– BeeOnRope
Nov 15 '18 at 23:26
2
2
The argument
-p doesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.– BeeOnRope
Nov 15 '18 at 23:26
The argument
-p doesn't exactly ignore errors: it invokes a different mode where any path components that don't exist are created (and hence it is not an error if happens that zero need to be created). The behavior is different since it will create components other than the last one, which may or not be desirable.– BeeOnRope
Nov 15 '18 at 23:26
add a comment |
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
add a comment |
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
add a comment |
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
answered Feb 5 '11 at 11:29
dogbanedogbane
193k66323374
193k66323374
add a comment |
add a comment |
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the-pargument makes the check unnecessary. You can still use it when you don't want to use-p, that is when you don't want all the parent directories to be created automatically.
– ADTC
Apr 13 '16 at 11:11
2
Actually it's even shorter to write[ -d /path/to/dir ] || mkdir /path/to/dir.. right side is executed when the left side is false.
– ADTC
Apr 13 '16 at 11:23
add a comment |
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the-pargument makes the check unnecessary. You can still use it when you don't want to use-p, that is when you don't want all the parent directories to be created automatically.
– ADTC
Apr 13 '16 at 11:11
2
Actually it's even shorter to write[ -d /path/to/dir ] || mkdir /path/to/dir.. right side is executed when the left side is false.
– ADTC
Apr 13 '16 at 11:23
add a comment |
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
answered Jun 9 '14 at 14:30
plesivplesiv
6,32332031
6,32332031
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the-pargument makes the check unnecessary. You can still use it when you don't want to use-p, that is when you don't want all the parent directories to be created automatically.
– ADTC
Apr 13 '16 at 11:11
2
Actually it's even shorter to write[ -d /path/to/dir ] || mkdir /path/to/dir.. right side is executed when the left side is false.
– ADTC
Apr 13 '16 at 11:23
add a comment |
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the-pargument makes the check unnecessary. You can still use it when you don't want to use-p, that is when you don't want all the parent directories to be created automatically.
– ADTC
Apr 13 '16 at 11:11
2
Actually it's even shorter to write[ -d /path/to/dir ] || mkdir /path/to/dir.. right side is executed when the left side is false.
– ADTC
Apr 13 '16 at 11:23
11
11
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
mmh, not cleaner: just shorter. It's difficult to understand the meaning of such a statement if you come across it.
– Davide Orazio Montersino
Jul 25 '14 at 7:46
I like this, although the
-p argument makes the check unnecessary. You can still use it when you don't want to use -p, that is when you don't want all the parent directories to be created automatically.– ADTC
Apr 13 '16 at 11:11
I like this, although the
-p argument makes the check unnecessary. You can still use it when you don't want to use -p, that is when you don't want all the parent directories to be created automatically.– ADTC
Apr 13 '16 at 11:11
2
2
Actually it's even shorter to write
[ -d /path/to/dir ] || mkdir /path/to/dir .. right side is executed when the left side is false.– ADTC
Apr 13 '16 at 11:23
Actually it's even shorter to write
[ -d /path/to/dir ] || mkdir /path/to/dir .. right side is executed when the left side is false.– ADTC
Apr 13 '16 at 11:23
add a comment |
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
add a comment |
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
add a comment |
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
answered Feb 5 '11 at 11:30
ivyivy
5,12112647
5,12112647
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4906579%2fhow-to-use-bash-to-create-a-folder-if-it-doesnt-already-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

3
Why do you have semicolons?
– ADTC
Apr 13 '16 at 11:05
1
The
;token is a command separator, so is newline. Asthenis a separate command, the preceding semicolon is needed to be be able to write it in the same line. The semicolons aftermkdirandfiare superflous.– Andreas Riedmüller
Jun 27 '18 at 7:26