Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

rch.pl recurses a filesystem, starting at a user-specified directory, ch(mod|own|grp)ing everything in it's path.   Additional arguments are user to chown to, and separate octal perms for directories and for files.

Use with caution.   Must be run as root, just like *nix ch(own|grp).   You can hose your *nix system with this if you're not careful, just like with *nix ch(mod|own|grp).   As a precautionary measure, it will cowardly refuse to recurse from '/'.

Scratches an itch that arose when I moved all my data files from shares on a Win2k box to Debian host w/Samba.   As always, comments and suggestions are welcome.

Update: Chagrined thanks to bluto and merlyn for pointing out *nix chmod and chown syntax I wasn't aware of.   I'm still glad I wrote this'un, as it scratches my one itch better, and also for having learned a thing or two about File::Find, and Pod::Usage as root.   This is (Perl|*nix), so there's more than one way of doing this.   8^)

#!/usr/bin/perl -w # rch.pl # pod at tail use strict; # avoid D'oh! bugs use User::pwent; # map username to uid/gid use File::Find; # recurse the filesystem $|++; # STDOUT hot my $user = shift; my $baseDir = shift; my $modeFile = oct(shift); my $modeDir = oct(shift); eXit() unless $< == 0; eXit() unless $baseDir && $user && $modeFile && $modeDir; eXit() unless grep /^[0-7]{1,4}$/, $modeFile, $modeDir; eXit() if $baseDir eq '/'; my $pw = getpwnam($user); eXit() unless($pw); my $uid = $pw->uid; my $gid = $pw->gid; sub chMOG { chmod($modeFile, $_) if(-f $_); chmod($modeDir, $_) if(-d $_); chown($uid, $gid, $_); } sub eXit { print "\nD'oh! Invalid/missing input or you're not root."; print "\nperldoc rch.pl and try again."; print "\nBut you can't perldoc as root."; print "\nSo there.\n\n"; exit; } find(\&chMOG, "$baseDir"); =head1 TITLE rch.pl - Recursively ch(mod|own|grp) files and directories =head1 SUMMARY rch.pl user base_dir file_mode dir_mode Base dir can be either relative or absolute, but *can't* be '/'. File and dir modes must be in octal, with or without the leading zero +. 0664 = user+group:rw other:r 0775 = user+group:rwx other:rx 0644 = user:rw group+other:r 0755 = user:rwx group+other:rx =head1 USAGE rch.pl user base_dir file_mode dir_mode =head1 COMMENTS =head1 TESTED User::pwent (no VERSION) File::Path (no VERSION) Perl 5.006.01 Debian 3.0 =head1 UPDATES 2002-10-05 09:15 CST Tone down caution, as is no more hazardous than *nix ch(mod|own|grp) Post to PerlMonks Subify abort messages+exit Try and rule out Pod::Usage "Superuser must not run perldoc without security audit and taint ch +ecks" Sanity-check user-supplied file/dir modes Automate name-to-uid/gid mapping Accept octal file & dir modes as commandline args 2002-09-18 14:50 CST Fix borken POD 2002-08-04 12:15 CST Initial working code =head1 TODO Sanity-check user-provided username =head1 CREDITS Props to davis, jwest, rob_au, tye, demerphq and zigdon for tips in t +he CB. And to some guy named vroom. =head1 AUTHOR ybiC =cut

In reply to (code) Recursive chmod chown chgrp in 30 lines by ybiC

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found