Only mkdir if it does not exist
本问题已经有最佳答案,请猛点这里访问。
我在我的bash脚本:P></
1 | mkdir product; |
当脚本运行超过一盎司的get the:P></
1 | mkdir: product: File exists |
在控制台。P></
我只知道如果看Run to the mkdir目录不存在。这是可能的吗?P></
做试验
1 | [[ -d dir ]] || mkdir dir |
或使用-p选项:
1 | mkdir -p dir |
1 2 3 | if [ ! -d directory ]; then mkdir directory fi |
或
1 | mkdir -p directory |
如果
使用mkdir的
1 2 3 4 | -p Create intermediate directories as required. If this option is not specified, the full path prefix of each oper- and must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner. |
-p, --parents
no error if existing, make parent directories as needed
试着用这个:
1 | mkdir -p dir; |
注意:这还将创建不存在的任何中间目录;例如,
查看mkdir-p
或者尝试一下:
1 2 3 4 5 | if [[ ! -e $dir ]]; then mkdir $dir elif [[ ! -d $dir ]]; then echo"$Message" 1>&2 fi |