如何创建子目录不存在递归使用bash? [英] How to create nonexistent subdirectories recursively using Bash?
本文介绍了如何创建子目录不存在递归使用bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个快速备份脚本,将倾倒一些数据库到一个不错的/整齐的目录结构,我意识到,我需要测试,以确保之前,我创建它们的目录是否存在。在code我的作品,但似乎有一个更好的方式来做到这一点。有什么建议?
[-d$ BACKUP_DIR] || MKDIR$ BACKUP_DIR
[-d$ BACKUP_DIR / $客户端] || MKDIR$ BACKUP_DIR / $客户端
[-d$ BACKUP_DIR / $客户端/ $年] || MKDIR$ BACKUP_DIR / $客户端/ $年
[-d$ BACKUP_DIR / $客户端/年$ / $月] || MKDIR$ BACKUP_DIR / $客户端/年$ / $月
[-d$ BACKUP_DIR / $客户端/ $年/月$ / $日] || MKDIR$ BACKUP_DIR / $客户端/ $年/月$ / $日
解决方案
您可以使用 -p
参数,这是的记录为:
-p,--parents
没有错误,如果存在,使父目录需要
块引用>所以:
的mkdir -p$ BACKUP_DIR / $客户端/ $年/月$ / $日
I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories exist before I create them. The code I have works, but it seems that there is a better way to do it. Any suggestions?
[ -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"
解决方案You can use the
-p
parameter, which is documented as:-p, --parents
no error if existing, make parent directories as needed
So:
mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
这篇关于如何创建子目录不存在递归使用bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文