Fellow Monks,
I have a simple question. What is the best way to handle creating directories which may possibly have parent directories that do not exist.I thought that mkdir would make any parent directories.
However, apparently it does not. It returns the error: No such file or directory. if any of the parent directories do not exist. I have a script Im working on that creates quite a few directories that may have parent directories that do not exist.
My solution is the following. Straining my brain to remember that pascal class I took in junior college, I seem to remember that this could be a good case for recursion. Of course Im no programmer by trade, so this may be ineficient. I dont know! Comments! Suggestions! What do you do??
#!/usr/bin/perl -w use strict; sub my_mkdir { my $path = shift; my $perms = shift; my ($parent) = $path =~ /(.*)\//; if ($parent) { my_mkdir($parent,$perms); mkdir ($path, $perms) or return; return 1; }else { #at base of path mkdir ($path, $perms) or return; return 1; } } my $path = shift; my $perms = 0777; my_mkdir($path, $perms) or die "ERROR: $!\n";
THANKS!
zzSPECTREz
In reply to recursive mkdir by zzspectrez
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |