|
Written by Jani Reinikainen
|
|
Nov 11, 2007 at 06:45 PM |
|
This is a simple script for chmodding/chowning file and directories recursively.
#!/usr/bin/perl
use strict;
# This script is designed to find and chmod and chown files and
# directories to different chmods and/or users.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# By JB Consulting Oy Ab / Jani Reinikainen [ jani (at) jbc (dot) fi ]
# Version 0.2.0 (19. February 2003). http://jbc.fi/
#######################################################
# These are some variables you probably want to set up.
my $dir_mode = 0755;
my $file_mode = 0644;
my $username = getpwnam(@ARGV[1]);
my $group = getgrnam(@ARGV[2]);
# No further editing neccessary.
#######################################################
# CHECK THAT WE GOT ALL NEEDED ARGUMENTS
if ($#ARGV < 0) {
print "Error: you need to give this script at least one argument.\n";
print "Usage: ./batch-chmod.pl \[path\] \[user\] \[group\]\n\n";
exit;
}
# FIRST FIND ALL DIRECTORIES
foreach my $directory (split("\n", `find @ARGV[0] -type d`)) {
chmod($dir_mode, "$directory") || die "Cannot chmod $directory: $!";
if (@ARGV[1] && !@ARGV[2]) {
chown($username, "$directory") || die "Cannot chown $directory: $!";
}
if (@ARGV[2]) {
chown($username, $group, "$directory") || die "Cannot chown $directory: $!";
}
print "$directory\n";
}
# NOW FIND ALL FILES
foreach my $file (split("\n", `find @ARGV[0] -type f`)) {
chmod($file_mode, "$file") || die "Cannot chmod $file: $!";
print "$file\n";
if (@ARGV[1] && !@ARGV[2]) {
chown($username, "$file") || die "Cannot chown $file: $!";
}
if (@ARGV[2]) {
chown($username, $group, "$file") || die "Cannot chown $file: $!";
}
}
|
Copyright © 2007 Jani Reinikainen. All rights reserved.
Permission granted to replicate information found on these pages, provided that all copyright headers/footers remain intact.