#!/usr/bin/perl -lw my $scriptissue = "Issue 1.0 26/09/04 John Cooper"; use File::Find (); use Cwd; # if no argument given, default to current directory my $startdir = cwd(); @ARGV = $startdir unless @ARGV; # if argument given , overwrite cwd $startdir = "@ARGV"; # convert /usr/bin to _usr_bin for filename ($startdir = $startdir) =~ s/\//_/g; # get the current date to append to output filename my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(tim +e); my $cyear = $year+1900; my $cmonth = $mon+1; if ( $cmonth < 10 ) {$cmonth = "0" . $cmonth;} my $cday = $mday; if ( $cday < 10 ) { $cday = "0" . $cday; } my $cdate = "_$cday" . "_$cmonth" . "_$cyear"; $txtfileout = "permrestore" . $startdir . $cdate; # subroutines sub time_as_date($) { my ($time) = @_; my $date = localtime($time); return $date; } sub print_info($) { my ($name) = @_; use File::stat; my $info = stat($name)my $size = $info->size; # size in bytes my $atime = $info->atime; # time of last access (seconds since epoch) my $mtime = $info->mtime; # time of last data modification my $ctime = $info->ctime; # time of last file status change # discard file type info from 'mode' and put in usual numeric format my $perms = sprintf("%04o", $mode & 07777); # convert times (in seconds since the epoch) to date strings my $adate = time_as_date($atime); my $mdate = time_as_date($mtime); my $cdate = time_as_date($ctime); + # print out the desired fields in tab-separated format print "\# ORIG PERM = $filename\t$uid\t$gid\t$perms\t$mdate"; print "chmod $perms $filename"; print "chown $uid:$gid $filename"; # write out to text file print TOUT "\# ORIG PERM = $filename\t$uid\t$gid\t$perms\t$mdate"; print TOUT "chmod $perms $filename"; print TOUT "chown $uid:$gid $filename"; } sub find(&@) { &File::Find::find } # Main programme print "\nPerl script to log filename permissions and owners"; print "which can be executed to re-establish them if they are changed" +; print "\nLog output to file $txtfileout"; print "Start directory is @ARGV"; print "\nUsage: logperm.pl [directory]"; print "\n Do you want to continue (y/n)? "; $answer = <STDIN>; if ( $answer =~ m/^[y,Y](.*)/ ) { open TOUT,">$txtfileout" or die "Cannot open output file $txtfileout : + $!\n"; *name = *File::Find::name; find { \&print_info($name) } @ARGV; close TOUT; my $mode = 0755; chmod $mode, $txtfileout; print "\nMake log file executable - chmod 755 $txtfileout"; print "Finished - $scriptissue"; } else { print "User answered no, log not created, quitting...." }
In reply to Owner Permissions restore script by johnc10
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |