Solved

Perl script to delete older files

Posted on 2016-11-08
6
69 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 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

12 Experts available now in Live!

Get 1:1 Help Now