I'd like to use the perl chown() command so I tried this:
@filenames = glob "/admin/new_account_page/*";
chown $uid, $gid, @filenames || die;
But it doesn't recurse through subdirectories. Is there a better way
to accomplish this than by using system()?
There is a simple work-around to your problem, but it's not really
all that elegant.
One of the nice things about the glob() function is that it can
take wildcards in the directory names, as well as in the file names.
So you can replace your line:
@filenames = glob "/admin/new_account_page/*";
with:
@filenames = glob "/admin/new_account_page/*";
push @filenames, glob "/admin/new_account_page/*/*";
push @filenames, glob "/admin/new_account_page/*/*/*";
push @filenames, glob "/admin/new_account_page/*/*/*/*";
push @filenames, glob "/admin/new_account_page/*/*/*/*/*";
push @filenames, glob "/admin/new_account_page/*/*/*/*/*/*";
and now you have recursion that's five levels deep.
Like I said, this isn't really all that elegant of a solution,
since your directory structure may have more (or less) than five
levels of recursion. If it had twenty levels of nested directories,
you'd have to have 21 total glob() lines to get all your files! If it
had only two levels of recursion, then three of those lines would be
useless.
There is a work-around for this problem, too. You can always put
the glob string in a loop that stops when it finds no more files, like
this:
$globString = "/admin/new_account_page/*";
while ( @globbedFiles = glob($globString) ) # note the single "="
{
push @filenames, @globbedFiles;
$globString .= "/*"; # for the next time through the loop
}
print "Found files: @filenames\n";
This approach should work, but I have to say that the standard
approach is to use the File::Find module, as it was meant to be used
for tasks just like yours. If you find the documentation for
File::Find to be a bit confusing, you can use the "find2perl"
executable (which you probably already have) to create most of the
code for you, with this command:
find2perl /admin/new_account_page -name "*"
Then all you'd have to do is put this line of yours in the wanted()
subroutine:
chown $uid, $gid, @filenames or die;
and then run that script.
I hope this helps, Randy.
-- Jean-Luc