May 142010
 

If you ever recursively chmod public_html to something terrible, this script will restore files back to their typical/default permissions.

cd to the public_html directory, and run the command:

perl

Hit enter, and then paste this code:

#!/usr/bin/perl -w
sub scanDir {
my $dir = shift;

opendir (DIR, $dir);
foreach my $file (readdir(DIR)) {
next if $file eq ".";
next if $file eq "..";

if (-d "$dir/$file") {
print "chmod directory: 0755 ($dir/$file)\n";
chmod (0755, "$dir/$file");
&scanDir ("$dir/$file");
}
else {
if ($file =~ /\.(cgi|pl|plx|plex)$/i) {
print "chmod CGI file: 0755 ($dir/$file)\n";
chmod (0755, "$dir/$file");
}
else {
print "chmod file: 0644 ($dir/$file)\n";
chmod (0644, "$dir/$file");
}
}
}
closedir (DIR);
}

&scanDir(".");
__END__

If it doesn’t execute right away, hit “Enter” again after the __END__ line. You should get output kinda like this:

chmod directory: 0755 (./mason-data/obj/2856374987/rsjs)
chmod file: 0644 (./mason-data/obj/2856374987/rsjs/chat.html.obj)
chmod file: 0644 (./mason-data/obj/2856374987/autohandler.obj)
chmod file: 0644 (./mason-data/obj/2856374987/list.html.obj)
chmod directory: 0755 (./mason-data/cache)
chmod directory: 0755 (./cookietest)
chmod CGI file: 0755 (./cookietest/login.cgi)
chmod file: 0644 (./cookietest/login.txt)
chmod file: 0644 (./cookietest/login.txt~)
chmod file: 0644 (./cookietest/login.cgi~)
chmod file: 0644 (./list.html~)
chmod file: 0644 (./xautohandler)
(etc. for each file and directory)

In a nutshell, it chmods all directories to 755, Perl/CGI scripts to 755, and normal files to 644.

 Posted by at 1:54 am