Solved

Perl script to delete older files

Posted on 2016-11-08
6
45 Views
Last Modified: 2016-11-12
Hi - I am storing all the files that come in a folder (example folderA), all the files have same name so I store them using time stamp after compressing them.
So for example folderA has the below files.
testfile2_20161102124335.zip
testfile2_20161102225357.zip
testfile2_20161102230409.zip
testfile2_20161102231907.zip
testfile2_20161102232440.zip
testfile2_20161102232811.zip
testfile2_20161102234703.zip
testfile2_20161102235149.zip
testfile2_20161102235825.zip
testfile2_20161103000507.zip
testfile2_20161103000659.zip
testfile2_20161106233202.zip

so if you observe i have the same filename but with different timestamp, i am storing these files as archive files and i want to automatically delete the old files if the count goes above 10. I mean if I have 10 files in the folder and if i get 11th file then i want to delete the oldest file before inserting the new one into the folder, so that there will always be 10 archived files at any time.

I did this using shell script as below but not able to do it in perl, so help me how to do this in perl.

for file in `ls $folderA`; do
    name=${file%.*}
noGzFile=`ls $folderA | grep $name | wc -l | awk '{print $1}'`
    #echo $noGzFile
    if [[ $noGzFile -gt 10 ]]; then
        noGzDel=$((noGzFile - 10))
        for f in `ls $folderC | grep $name | sort | head -$noGzDel`; do
            rm $folderC/$f
        done
    fi
done

Open in new window

0
Comment
Question by:shragi
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 41878957
What have you tried?
What warnings and errors do you get?
What part of the task do you not know how to do?

Please post your code.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 41879831
This should do what you want:
use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my @files = sort grep m{^testfile2_\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file
# move it to the directory

Open in new window

0
 

Author Comment

by:shragi
ID: 41882276
Hi wilcoxon - the script works i made one simple change to the script and got the below error, can you fix it please

Global symbol "$fnameNoExt _" requires explicit package name at deleteOldFiles.pl line
6.
Execution of deleteOldFiles.pl aborted due to compilation errors.
Press any key to continue . . .

Open in new window



use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my $fnameNoExt = "testfile2";
my @files = sort grep m{^$fnameNoExt _\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 28

Expert Comment

by:FishMonger
ID: 41882293
Undo the "simple change" you made which you didn't show us.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 41882306
I don't see what is causing that error in the code you posted but I do see another error.  This may fix your issue (if the code you posted was not exact).
use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my $fnameNoExt = 'testfile2';
my @files = sort grep m{^${fnameNoExt}_\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file

Open in new window

0
 

Author Comment

by:shragi
ID: 41882319
Thank you Wilcoxon that fixed my error
grep m{^${fNameNoExt}_

Using additional bracket for variable fixed my error.

Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now