我正在创建一个快速备份脚本,将一些数据库转储到一个漂亮/整洁的目录结构中,我意识到我需要测试以确保在创建目录之前存在这些目录。我的代码有效,但似乎有更好的方法。有什么建议?
[ -d "$BACKUP_DIR" ] || mkdir "$BACKUP_DIR"
[ -d "$BACKUP_DIR/$client" ] || mkdir "$BACKUP_DIR/$client"
[ -d "$BACKUP_DIR/$client/$year" ] || mkdir "$BACKUP_DIR/$client/$year"
[ -d "$BACKUP_DIR/$client/$year/$month" ] || mkdir "$BACKUP_DIR/$client/$year/$month"
[ -d "$BACKUP_DIR/$client/$year/$month/$day" ] || mkdir "$BACKUP_DIR/$client/$year/$month/$day"
您可以使用-p
参数,即documented as:
-p, - 父母
如果存在则没有错误,根据需要创建父目录
所以:
mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
$ mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
虽然现有的答案肯定能解决目的,如果你想在两个不同的子目录下复制嵌套目录结构,那么你可以这样做
mkdir -p {main,test}/{resources,scala/com/company}
它将在调用它的目录下创建以下目录结构
├── main
│ ├── resources
│ └── scala
│ └── com
│ └── company
└── test
├── resources
└── scala
└── com
└── company
该示例来自this link,用于创建SBT目录结构