How to use Bash to create a folder if it doesn't already exist?

前端 未结 6 1324
暖寄归人
暖寄归人 2020-12-02 06:43
#!/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?<

6条回答
  •  眼角桃花
    2020-12-02 07:25

    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

提交回复
热议问题