#!/usr/bin/perl =pod pchmod - perl recursive chmod by: scott@furt.com nothing in life is guaranteed, especially not this code. use it however you want. if you like it, perhaps send me an email and tickle my ego :-) =cut use strict; use vars qw($dir $rec $test $verb $igdir $foll $c $help); use File::Find; my $DBG = 0; #print out some misc. information my $VERSION = 2; #approx. version number #turn verbosity ON by default $verb++; # # some configurations (chosen with -c option) # my %cfg = ( 'default' => { 'dfile' => '0644', #default file mode 'ddir' => '0755', #default directory mode 'modes' => { 'cgi' => '0755', #mode for .cgi files 'pl' => '0755', #mode for .pl files 'sh' => '0755', #mode for .sh files }, }, 'group' => { 'dfile' => '0664', 'ddir' => '0775', 'modes' => { 'cgi' => '0775', 'pl' => '0775', 'sh' => '0775', }, }, 'secure' => { 'dfile' => '0600', 'ddir' => '0700', 'modes' => {}, }, ); # ----------------------------------------- #get options from cmd line while ( my $opt = shift ) { $rec++ if (($opt eq '-r')||($opt eq '-R')); $test++ if (($opt eq '-t')||($opt eq '-T')); $igdir++ if (($opt eq '-i')||($opt eq '-I')); $verb-- if (($opt eq '-V')||($opt eq '-v')); $foll++ if (($opt eq '-s')||($opt eq '-s')); $help++ if ($opt =~ /^(-?)-h/i); $dir = $opt if ($opt !~ /^\-/); $c = $1 if ($opt =~ /^\-c(.*)/); } #default directory is the current one. $dir ||= '.'; #default config to use is 'default' $c ||= 'default'; #set variables to selected config's default file and dir mode my $dfile = $cfg{$c}{'dfile'}; my $ddir = $cfg{$c}{'ddir'}; #print out "help" message (if help ON) if ($help) { &help(); } #spew some info about current options (if verbosity ON) if ($verb) { &show_env(); } # ----------------------------------------- #start your engines. find ( { 'wanted' => sub { &wanted($_); }, 'follow' => $foll, #follow symlinks? 'no_chdir' => 1, #dont execute a chdir() when # it goes into subdirectories }, $dir #directory to start processing in ); exit; sub wanted { my ($name) = @_; my ($vname) = $File::Find::name; if ($DBG) { print "Name: $name\n"; print "Dir: ". $File::Find::dir ."\n"; print "Topdir: ". $File::Find::topdir ."\n"; } if (!$rec) { return if ($File::Find::topdir ne $File::Find::dir); } print "Attempting: [$name] " if ($DBG); #looking at a file if (-f $name) { $name =~ /.*\.(.*?)$/; if ($cfg{$c}{'modes'}{$1}) { my $mode = $cfg{$c}{'modes'}{$1}; print "chmod $mode $vname\n" if ($test || $verb); chmod (oct($mode), $name) if (!$test); return 1; } #chmod to default value #if not already chmodded chmod(oct($dfile), $name) if (!$test); print "chmod [$dfile] $vname\n" if ($test || $verb); } #looking at a directory elsif ( (-d $name) && ($name ne '.') && ($name ne '..') ) { chmod (oct($ddir), $name) if (!$test || !$igdir); print "chmod /$ddir/ $vname\n" if (($test || $verb) && !$igdir) +; } } # show current options sub show_env { print "Using Options: * Dir $dir * Config: '$c' * Recursing ". (($rec) ? "ON" : "OFF") ." * Verbosity ". (($verb) ? "ON" : "OFF") ." * Ignore Dirs ". (($igdir) ? "ON" : "OFF") ." * Test Only ". (($test) ? "ON" : "OFF") ."\n "; } #show the help message sub help { print "perl chmod (v$VERSION) Usage: pchmod <dir> [options] -r\t\tRecursive mode -v\t\tQuiet mode, doesnt print status messages -s\t\tFollow sym.links (NO by default) -i\t\tDo not chmod() directories -t\t\tTest mode, doesnt alter any files -c <string>\tUse <string> config values Configs available:\n"; map { print "\t\t-c$_\n"; print "\t\t\tdfile: $cfg{$_}{'dfile'}\n"; print "\t\t\tddir: $cfg{$_}{'ddir'}\n"; } (keys %cfg); exit(0); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: recursive perl chmod
by a (Friar) on Mar 02, 2001 at 10:00 UTC | |
by howard40 (Beadle) on Mar 03, 2001 at 00:28 UTC | |
by ultranerds (Hermit) on Jun 22, 2009 at 09:22 UTC | |
(ichimunki) re: recursive perl chmod
by ichimunki (Priest) on Mar 02, 2001 at 17:41 UTC | |
by howard40 (Beadle) on Mar 03, 2001 at 00:42 UTC |