Advertisement
zefie

High-speed recursive permission changer v3

Apr 2nd, 2012
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.34 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # High speed recursive permission changer
  4.  
  5. $pwd = `pwd`;
  6. $pwd = s/ /\\ /g;
  7.  
  8. $dmode = @ARGV[0];
  9. $fmode = @ARGV[1];
  10. $user = @ARGV[2];
  11. $group = @ARGV[3];
  12. $verbose = @ARGV[4];
  13.  
  14. if (!$fmode || !$dmode) {
  15.     print "Usage: perms.pl dirmode filemode [user] [group] [-v]\n";
  16.     exit();
  17. }
  18.  
  19. $files = `find $pwd`;
  20. @files = split("\n",$files);
  21.  
  22. $i = 0;
  23. $j = 0;
  24.  
  25. if ($user && $group) {
  26.     ($login,$pass,$uid,$gid) = getpwnam($user);
  27.     my $username = getpwuid( $< );
  28.     if ($username ne "root") {
  29.         die("Changing owner:group requires root.");
  30.     }
  31.  
  32.     $gid   = getgrnam($group);
  33.  
  34.     if (!$uid) {
  35.         print "User \"".$user."\" does not exist!\n";
  36.         exit();
  37.     }
  38.     if (!$gid) {
  39.         print "Group \"".$group."\" does not exist!\n";
  40.         exit();
  41.     }
  42. } else {
  43.     $verbose = @ARGV[2];
  44. }
  45.  
  46. foreach $file (@files) {
  47.     if (-d $file) {
  48.         if ($verbose) {
  49.             print "Setting permissions (".$dmode.", ".$uid.":".$gid.") on $file...\n";
  50.         }
  51.         chmod oct($dmode), $file;
  52.         if ($uid && $gid) {
  53.             chown $uid, $gid, $file;
  54.         }
  55.     }
  56.     else {
  57.         if ($verbose) {
  58.             print "Setting permissions (".$fmode.", ".$gid.":".$uid.") on $file...\n";
  59.         }
  60.         chmod oct($fmode), $file;
  61.         if ($uid && $gid) {
  62.             chown $uid, $gid, $file;
  63.         }
  64.     }
  65.     if (!$verbose) {
  66.         $i++;
  67.         print "Set permissions on ".$i." objects...";
  68.         print "\r";
  69.     }
  70. }
  71.  
  72. if (!$verbose) {
  73.     print "\n";
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement