Here's something i put together over the past two days to solve a problem with my SSH client's file uploader (it sets permissions screwy, and i was constantly having to change all the permissions by hand whenever i uploaded a file or directory tree). perhaps it will be of some service to some of you working with linux/unix dealing with a constant need to chmod large amounts of files (it supports recursion through an entire directory tree, which is mainly why i wrote it).

save it as 'pchmod.pl' and type 'pchmod.pl -h' to get a the 'help' menu.
#!/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); }

In reply to recursive perl chmod by howard40

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.