[Wellington-pm] chmod, chown and friends.

Grant McLean grant at mclean.net.nz
Fri Aug 20 03:41:12 CDT 2004


On Fri, 2004-08-20 at 18:48, Enkidu wrote:
> Hi all,
> 
> Do these commands support the -R flag as Perl functions?

No the Perl built-in functions don't support recursion.
This is definitely a case where the specialised command line 
tools are a better fit for common tasks.

> If not, is there a cool way of simulating the shell command:
> 
> chown -R user:group directory (and similar)?

Don't be afraid to do:

  system("chown -R user:group directory");

For portability reasons, I don't usually recommend using system 
when Perl built-ins can do the job.  But any platform that 
supports Unix chown semantics probably has a utility called chown 
in the path.

The File::Find module that is included in the core Perl 
distribution can be used for recursing through directory trees. 
Unfortunately is has a very poorly designed API, so I tend to 
avoid it.

If I was going to use a module for recursing, I'd tend to use
File::Find::Rule from CPAN:

  use File::Find::Rule; 
  chown($uid, $gid, File::Find::Rule->in($directory));

That should find everything in $directory.

You can build more complex searches by chaining the methods 
together.  For example to find all JPEG files > 1MB

  File::Find::Rule->file()
    ->name('*.jpg')
    ->size('>1Mi')
    ->in($directory);

By the way Cliff, your post was held up in the list admin queue
because it was sent to the old list address (ie: before we got 
switched from Majordomo to Mailman).  The new address is just

    wellington-pm at mail.pm.org

Regards
Grant




More information about the Wellington-pm mailing list