How do I recursively set read-only permission with Perl?

I would like $direverything below it to be read only. How can I install this using Perl?

+3
source share
3 answers

You can do this with a combination of File :: Find and chmod (see perldoc -f chmod ):

use File::Find;

sub wanted
{
    my $perm = -d $File::Find::name ? 0555 : 0444;
    chmod $perm, $File::Find::name;
}
find(\&wanted, $dir);
+9
source
system("chmod", "--recursive", "a-w", $dir) == 0
  or warn "$0: chmod exited " . ($? >> 8);
+3
source

, . ,

set_perms($dir);

sub set_perms {
     my $dir = shift;
     opendir(my $dh, $dir) or die $!;
     while( (my $entry = readdir($dh) ) != undef ) {
          next if $entry =~ /^\.\.?$/;
          if( -d "$dir/$entry" ) {
              set_perms("$dir/$entry");
              chmod(0555, "$dir/$entry");
          }
          else {

              chmod(0444, "$dir/$entry");
          }
     }
     closedir($dh);
}

, Perl:

system("find $dir -type f | xargs chmod 444");
system("find $dir -type d | xargs chmod 555");

xargs, .

+1
source

Source: https://habr.com/ru/post/1765353/


All Articles