views:

379

answers:

2

Hello all,

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"
+21  A: 

Um, how about just using mkdir -p ?

bmargulies
@bmargulies - Holy crap that was way simpler than I thought =P
Topher Fangio
+3  A: 
$ mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
Jonathan Feinberg
+1 for the example.
mskfisher