Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exclude hidden file in a Cleanup Script

Status
Not open for further replies.

morijona

IS-IT--Management
Nov 6, 2003
10
0
0
CA
Hi i got a script to cleanup file older than X day but i'm unable to exclude every file and folder starting with a (.)
Anyone can help me ?

Here's my script.

use Getopt::Long;
use File::Find;

Getopt::Long::Configure("bundling");
GetOptions("time|t=n", "noprompt|n", "recursive|R", "help|?");

if (!defined($directory = pop) || defined($opt_help)) {
print qq{
Usage: cleanup.pl [OPTIONS] [DIRECTORY]
Cleanup a directory by deleting files older than a given day.

-t, --time=N delete files older than N days
-n, --noprompt do not prompt before deleting
-R, --recursive delete files in all subdirectories too
--help display this help and exit
};
exit(0);
}

$days = defined($opt_time) ? $opt_time : 14; # defaults to two weeks

sub process_file {
if ( $_ = ".*" ) {next;}
elsif ( -f && -M _ > $days){push @files, $_;}
}

if (defined($opt_recursive)) {
find({ wanted => \&process_file, no_chdir => 1 }, ($directory));
} else {
opendir(DIR, $directory) or die "can't opendir $directory: $!";
for (readdir(DIR)) {
$_ = "$directory/$_";
&process_file;
}
}

unless (defined($opt_noprompt)) {
print "Files older than $days:\n".join "\n",@files;
print "Delete (Y/N)?";
exit (0) if (lc <STDIN> ne "y\n");
}

unlink @files;



Thanks.
 
It look as if you are asking to replace this line:
Code:
if ( $_ = ".*" ) {next;}
If so, try this:
Code:
next if $_ =~ /^\.\.?$/;
 
Why not something simple like:

next if $file=/^\./;

Think that should work :-S.
Just checks to see if the first character is a "."
 
Beat me to it Xagte :-D

Whats the last bit for? Lost me there.
I'm not too good at regex. I should read up on it.
 
one way:

Code:
sub process_file {
next if ( $_ eq '.' or $_ eq '..');    
if ( -f && -M _ > $days){push @files, $_;}    
}
 
better to use proper syntax:

next if $file =~ /^\./;

you had '=' which is the assingment operator instead of the string binding operator: =~

but that might get some files that start with a dot, such as htaccess files, so better to go the extra step:

next if $file =~ /^\.{1,2}$/;

or as Xaqte posted:

next if $_ =~ /^\.\.?$/;
 
Thanks for the quick answer but i have the same problem with each solution.

it want to delete those file.

i run the command
perl script.pl -t 15 -R /home/FTP

i got this:

Files older than 15:
/home/FTP/expedition/.bashrc
/home/FTP/expedition/.gtkrc
/home/FTP/expedition/.zshrc
/home/FTP/expedition/.emacs
/home/FTP/expedition/.bash_logout
/home/FTP/expedition/.canna
/home/FTP/expedition/.bash_profile
/home/FTP/expedition/.kde/Autostart/.directory
/home/FTP/expedition/.kde/Autostart/Autorun.desktop
/home/FTP/sales/.bashrc
/home/FTP/sales/.gtkrc
/home/FTP/sales/.zshrc
/home/FTP/sales/.emacs
/home/FTP/sales/.bash_logout
/home/FTP/sales/.canna


thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top