×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

delete file with similar names

delete file with similar names

delete file with similar names

(OP)
Hi,
I guess this might come down to more of a unix command then perl question. I want to write a script that will delete any file with similar name for the particular month.
The files under the directory are name such as below:
-rw-r----- 1 oracle dba 20353024 Jul 3 23:00 exprmanFri_0703152300.dmp
-rw-r--r-- 1 oracle dba 6066 Jul 3 23:00 exprmanFri_0703152300.log
-rw-r----- 1 oracle dba 20418560 Jul 10 23:00 exprmanFri_0710152300.dmp
-rw-r--r-- 1 oracle dba 6067 Jul 10 23:00 exprmanFri_0710152300.log
-rw-r----- 1 oracle dba 19423232 Jul 17 23:00 exprmanFri_0717152300.dmp
-rw-r--r-- 1 oracle dba 6067 Jul 17 23:00 exprmanFri_0717152300.log

My script is below:
#!/usr/bin/perl
$dir = '/apps/oracle/admin/gcprod/dpdump';
print "Enter the two digit month number for the file to be deleted: ";
$mon = <STDIN>;
chomp($mon);
opendir DIR, $dir;
foreach my $file (readdir(DIR)) {
$len = length $file;
$str1 = substr($file, $len-14, 2);
next if $file =~ /^\./; #skip ., .., etc
next unless -f "$dir/$file"; # skip non-files
if ($str1 = $mon)
{
system ("rm");
}
# print "The month of the file is $str1 \n"; for debug purpose
}
closedir DIR;

I am not sure how I should write the rm command the remove the files I wanted. Please advise. Will rm exprman*_$mon* work? I don't have a test system to test this. So, do not want to issue this and delete any files that I actually needed.

RE: delete file with similar names

I might do something like this:

CODE

use warnings;
use strict;
use File::Find;

my $dir = './txt/';	# Change this to your directory
my $month = '07'; 	# Get this from STDIN, command line, etc.
find( {wanted => \&remove_files_by_month, follow => 0}, $dir);

sub remove_files_by_month() {
	if (-f $_) {
		if (substr($_, length($_)-14, 2) eq $month) {
			print "unlink $_\n";	# Use this for testing
			#unlink $_;	# Uncomment this line to delete files
		}
	}
} 
And you can nearly always test to some degree; in this case the script prints the commands that would have run (see the comments.) Alternately, you could copy a representative set of log files to a temporary location in your home directory or somewhere else and operate on the copies instead of the originals.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close