CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Recursively delete *.bak files



Feb 6, 2012, 7:21 AM

Post #1 of 5 (1403 views)
Recursively delete *.bak files Can't Post

System: Sun Solaris and Perl 5.8.8

I need to delete all *.bak files recursively. Actually this is a test program to delete files that start with ".", which are some type of backup file for Adobe Photoshop or Illustrator. But I'm starting writing my own program to delete *.bak files. Why? Because unix 'find' does not delete any files that begin with ".".

Also note, I cannot use any Perl packages which use the unix 'find' command due to its limitations.

Here is my core program which checks to see if the file is a file or directory.

sub procdir 
{my($curdir)=@_; # procdir


$s="\n$procname: Going into dir _".$curdir."_, testing $myspec";

opendir(MYDIR, $curdir) or die "ERROR $procname: could not open dir $curdir";
# Get one file at a time.
while ( defined($file = readdir(MYDIR)) )
#$d=~s/\///g; # Drop slashes.
#$s="Testing entry $fullfile against $myspec"; # DEBUG
#writeerr($s); # DEBUG
if (-f $fullfile)
$s="$procname: Checking file $fullfile";

#if ($fullfile=~m/$myspec/)
if ($fullfile=~m/\*\.bak$/) # <==PROBLEM HERE
$s=" Deleting $fullfile";
#unlink($fullfile) or die "ERROR $procname: Could not delete file $fullfile";
elsif (-d $fullfile) # If it's a directory
# Ignore current and parent directories.
if ($file!~m/^(\.|\.\.)$/)
procdir($tdir); # Process directory
#else {
# $s="ERROR $procname: Unknown file type: $file";
# writeerr($s);
# }
} # while

return; # procdir

My problem is with this line:
if ($fullfile=~m/\*\.bak$/) # <==PROBLEM HERE

When I get to a file named "testdir/dir1/test1.bak" the file should get deleted, but it does not past the IF statement. Why?

Program is called from shell like this:
perl testdir \*\.bak

$myspec is ARG[1], the filespec, or *.bak.


(This post was edited by bulrush on Feb 6, 2012, 7:24 AM)


Feb 6, 2012, 12:11 PM

Post #2 of 5 (1395 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

You are confusing regular expressions with shell wild cards.

You can either read the directory with glob (perldoc -f glob) or read it with readdir and parse the filename strings with regular expressions (perldoc perlre)

The expression argument for glob can contain unix shell wildcards.
Good Luck,


Feb 7, 2012, 4:34 AM

Post #3 of 5 (1384 views)
Re: [BillKSmith] Recursively delete *.bak files [In reply to] Can't Post

I am using readdir(), doesn't that return a string? I then construct a string ($fullfile) which has the relative path of the filename. So why isn't my regex comparison of the string ($fullfile) filename working?

(This post was edited by bulrush on Feb 7, 2012, 4:35 AM)


Feb 7, 2012, 6:38 AM

Post #4 of 5 (1378 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

Your regular expression would match any string (file name) that ends in '*.bak'. The star is not a valid filename charachter, so your expression never matches anything.

You need a regular expression that matches any file name that ends in '.bak'. This task has nothing to do with the shell or wildcards. Remove '\*' from your regular expression.
Good Luck,

Veteran / Moderator

Feb 7, 2012, 7:05 AM

Post #5 of 5 (1375 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

Why waste your time reinventing the wheel? Use either Perl's File::Find or File::Find::Rule module.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives