如何创build不存在的子目录recursion地使用Bash?
我正在创build一个快速备份脚本,将一些数据库转储到一个很好/整洁的目录结构,我意识到我需要testing,以确保目录存在之前,我创build它们。 代码我有工作,但似乎有一个更好的方法来做到这一点。 有什么build议么?
[ -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
参数,该参数logging为 :
-p, – 父母
没有错误,如果存在,根据需要制作父目录
所以:
mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
$ mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"