Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

(code) Recursive chmod chown chgrp in 30 lines

by ybiC (Prior)
on Oct 04, 2002 at 21:01 UTC ( #202898=snippet: print w/ replies, xml ) Need Help??
Description: 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

# 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 and try again.";
  print "\nBut you can't perldoc as root.";
  print "\nSo there.\n\n";

find(\&chMOG, "$baseDir");

=head1 TITLE - Recursively ch(mod|own|grp) files and directories

=head1 SUMMARY 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 user base_dir file_mode dir_mode


=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
 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



Comment on (code) Recursive chmod chown chgrp in 30 lines
Download Code
Replies are listed 'Best First'.
Re: Recursive chmod chown chgrp in 30 lines
by jj808 (Hermit) on Oct 04, 2002 at 22:01 UTC
    I realise that TMTOWTDI, but I don't really understand what you are gaining over the standard UNIX chown/chgrp/chmod commands, as these are recursive anyway (with the -R option)?

    I'm not by any means being anti-Perl here, but if there's a perfectly good system command then why not use it? Even if you want all the operations to be completed in one go, a four line shell (or Perl) script would suffice.

    And if the system commands are not available, just install PPT.


Re: Recursive chmod chown chgrp in 30 lines
by bluto (Curate) on Oct 04, 2002 at 23:15 UTC
    I guess I'd have to agree with jj808.

    You would have to have a serious number of files under unix (say 50K), or a seriously brain dead OS to make this worth the risk. One problem I see is that all of your files will have the same permissions set. So they will all be executable or all non-executable. The other problem is that you are executing every operation even if it isn't needed, though I doubt that slows things down much.

    In general, you can do all of this with two commands (since you generally want the directory's permissions to be similar to a file's permissions, except for the 'x' bit)...

    # chown -R newuser:newgroup /my/dir # chmod -R ug+rwX /my/dir

    ... (where 'X' means put an x bit on it if it already has an x bit set) on any reasonable unix system.


Re: Recursive chmod chown chgrp in 30 lines
by Abigail-II (Bishop) on Oct 07, 2002 at 11:59 UTC
    Note that in some Unix systems, users are allowed to chown() on their own files, and "give them away". And on most (if not all) Unix systems, users are allowed to "chgrp" files provided they
    1. Own the file.
    2. Are a member of the current group owner of the file.
    3. Are a member of the new group owner of the file.

    Several years ago, I wrote the "chmod" command of the Perl Power Tools (PPT). It was interesting to see that "chmod" was implemented differently on all Unix systems I tested (and often, the manual differed from the implementation as well). Figuring out what chmod = file does depending on the permissions of the file (or directory) on the various operating systems was interesting.


(tye)Re: Recursive chmod chown chgrp in 30 lines
by tye (Sage) on Oct 04, 2002 at 23:30 UTC

    I guess I'd have to agree with bluto. I realise that TMTOWTDI, but I don't really understand why you would bother to write this when you have chmug!

            - tye (but it is pronounced "sarkazim")
Re: (code) Recursive chmod chown chgrp in 30 lines
by Anonymous Monk on Oct 16, 2004 at 01:26 UTC
    I found this code particularly useful and elegant. The use of the find command with the subroutine really made a hard task easy. I did not use all of it, but there are definitely some nice things happening here.

    I found one reason to use this type of thing instead of chown/chgrp/chmod is because this is more portable. As long as perl is installed this script will work where as the chown/chgrp/chmod commands may not be on every system that runs perl. This allows me to only require perl for my users.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://202898]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2016-05-05 19:55 GMT
Find Nodes?
    Voting Booth?