Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

deleting a file in perl

by keralaqueen234 (Initiate)
on Apr 02, 2013 at 06:05 UTC ( #1026587=perlquestion: print w/replies, xml ) Need Help??
keralaqueen234 has asked for the wisdom of the Perl Monks concerning the following question:

i have a requirement where in i need to delete all the old log files in a folder.Log file will be having the extn of .log. But the thing is recent log file need not be deleted . For example if i have a folder say xyz and inside that foleder i have 3 log files named 1)currentlog.log 2)currentlog_2010.log 3)currentlog_2011.log and some other .txt files with some names are also present.please note , currentlog.log is the recent logfile .So this need not be deleted. i need to delete all the other log files that has the name pattern 'currentlog*' .Pleasenote ,the recent logfile and other .txt files should be retained.on trigering the perlscript only currentlog_2010.log ,currentlog_2011.log should get deleted.How to do this?

Replies are listed 'Best First'.
Re: deleting a file in perl
by hdb (Monsignor) on Apr 02, 2013 at 06:17 UTC

    Below is the Perl equivalent of "rm currentlog_*.log" or "del currentlog_*.log" on the command prompt:

    unlink glob "currentlog_*.log";

      i tried with unlink glob. but it deleted my currentlog file also :( ..... i dont want to delete it .eventhough i gave currentlog*.log, it deleted currentlog.log too. Please help me out

        currentlog.log fits the pattern currentlog*.log. The "_" makes the difference. If you need something more general than "currentlog_*.log" try this:

        my @logs = glob "currentlog*.log"; for (@logs) { next if $_ eq "currentlog.log"; unlink $_; }

        I hope you have copies of your logfiles before do any testing...

Re: deleting a file in perl
by CountZero (Bishop) on Apr 02, 2013 at 06:18 UTC
    Check out File::Find::Rule. You can define the files to be found (and then deleted) with a regular expression.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: deleting a file in perl
by kcott (Chancellor) on Apr 02, 2013 at 09:10 UTC

    G'day keralaqueen234,

    Welcome to the monastery.

    You showed a set of requirements in your initial post (files: currentlog.log, currentlog_2010.log and currentlog_2011.log). You didn't follow the advice given by hdb (using currentlog*.log instead of currentlog_*.log) and seemed surprised it didn't work. Further on, you changed your requirements showing a completely different set of filenames to match: "i have files with names currentlog.log,currentlog123.log,currentlog233.log,detail.txt,see.txt" [sic].

    Any changes you make to requirements in your original posting (OP) should be clearly shown by adding an Update section to your OP. You should fix up your OP now.

    This appears to be fast becoming an Off Topic thread with the problem being your lack of knowledge of your operating system rather than anything to do with Perl.

    Assuming nothing else has changed between reading this thread and posting this response, it would appear that the pattern you want is: currentlog?*.log

    $ ls -1 currentlog.log currentlog123.log currentlog233.log currentlog_2010.log currentlog_2011.log detail.txt see.txt $ ls -1 currentlog?*.log currentlog123.log currentlog233.log currentlog_2010.log currentlog_2011.log $ perl -E 'say for glob "currentlog?*.log"' currentlog123.log currentlog233.log currentlog_2010.log currentlog_2011.log

    -- Ken

Re: deleting a file in perl
by 2teez (Priest) on Apr 02, 2013 at 06:44 UTC

    Something like this using File::Find, which IMHO is an overkill for what you need,
    since you are just going through one directory, you could consider using opendir, if you want.

    use warnings; use strict; use File::Find qw(find); die "No directory is specified" unless defined $ARGV[0]; my $dir = $ARGV[0]; find( \&unwanted, $dir ); sub unwanted { if (/^\.{1,}/) { return } else { print "Do you want to delete ", $_, $/; chomp( my $ans = <STDIN> ); unlink $_ if $ans =~ /\by\b/; } }
    NOTE: Deleting a file could be both dangerous and so painful if in error, a wrong file is deleted, hence the need to check the file before deleting.
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      could you please tell me how exactly do i do the coding for this? i need to first see which all files in the diectory has the file named currentlog*.log . and i need to delete all the files with that name pattern,but need to retain currentlog.log(which is the latest log file ) ..pleasee help me :(

        The script shown in my previous post, should work.
        Usage: perl <directory_name>
        Where is your Perl script, directory_name the directory you which to go through, checking which file to delete, after you have been shown the name of the file, if you want to delete the file you answer y, if not just press ENTER.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: deleting a file in perl
by pvaldes (Chaplain) on Apr 02, 2013 at 09:04 UTC

    This is a way, but not the only way, (and perhaps not the best way)


    If you are in a Linux OS consider to use find2perl to search for old files, maybe copy the newest to a different place, and delete files if its filename match AND its timestamp is older than X days or months. See mtime, atime, etc.


    manage your log files with a specific module. Look here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026587]
Approved by Athanasius
[choroba]: I sent him the invitations for the last two meetings, but he didn't arrive
[choroba]: ^ should have been a /msg

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-09-25 15:35 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (280 votes). Check out past polls.