244
#!/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?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
mlzboy
  • 14,343
  • 23
  • 76
  • 97
  • 4
    Why do you have semicolons? – ADTC Apr 13 '16 at 11:05
  • 2
    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 07:26

7 Answers7

392

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 for mkdir, this check is useless, because this is what it does in the first place. Just write:

mkdir -p /home/mlzboy/b2c2/shared/db;

and that's it.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
Maxim Sloyko
  • 15,176
  • 9
  • 43
  • 49
  • 6
    Note: the `-p` flag causes any parent directories to be created if necessary. – Danijel May 15 '18 at 08:03
  • 55
    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 02:11
  • 10
    Wow! Knowing that "[" is a command in bash is such an eye-opener. I feel like a lot of issues with my bash scripts are now resolved! – raluru Nov 05 '19 at 11:13
  • 3
    Incredible to find out that "[" is a command. That's made a lot of things click for me that didn't make sense before. Thank you! – Matt Zabojnik Sep 28 '20 at 14:52
  • How to check using Regex to identify the folder? Ex: if [ ! -d /home/mlzboy/b2*/shared/db ]; – Jagadeesan G Nov 11 '21 at 07:42
  • @mlzboy I've seen plenty of SO questions for which none of the answers warranted being accepted as the correct answer. This is not such a case. – Bob Kline Feb 10 '22 at 20:09
  • [ is a special characters (meta-characters) used for defining regular expressions https://berkeley-scf.github.io/tutorial-using-bash/regex.html – Adam Jul 28 '23 at 15:53
125

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
kurumi
  • 25,121
  • 5
  • 44
  • 52
94

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

Automatico
  • 12,420
  • 9
  • 82
  • 110
  • 8
    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
29

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
dogbane
  • 266,786
  • 75
  • 396
  • 414
20

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
plesiv
  • 6,935
  • 3
  • 26
  • 34
  • 14
    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 07:46
  • 1
    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
  • 4
    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
5

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;
ivy
  • 5,539
  • 1
  • 34
  • 48
0

Create your directory wherever

OUTPUT_DIR=whatever

if [ ! -d ${OUTPUT_DIR} ]
then
    mkdir -p ${OUTPUT_DIR}
fi
Jason
  • 11