Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Unlink works in windows, not in Linux?

by jagexCoder (Novice)
on Dec 12, 2012 at 00:31 UTC ( #1008432=perlquestion: print w/replies, xml ) Need Help??
jagexCoder has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a server that creates log files of with certain file names, I'm use regex to detect these filenames and based on them delete the logs that are older than 24 hours. The script works great tested on Windows XP, however in Linux it does not work. The backup files are not deleted and the log does not mention any files were deleted. Also the $unlink does not display the warning message. Here's the relevant part of the code:
until ($log_written != 0) { print FILE "*** SCRIPT RUN ON: " . $dt . " ***\n"; print FILE "Deleting backup files more than 1 day old:\n"; foreach my $file (<grm_backup-*_*_*_*_*_*_EST_*.bak>) { if ( -M $file > 2 ) { #print "Deleting the log file more than 1 days old: " . $f +ile; unlink $file or die "\nCould not delete the backup $file: +$!"; print FILE "Deleted -> " . $file . "\n"; } } foreach my $file2 (<backup_db-*_*_*_*_*_*_EST_*.bak>){ if ( -M $file2 > 2 ) { #print "Deleting the log file more than 1 days old: " . $f +ile2; unlink $file2 or die "\nCould not delete the backup $file2 +: $!"; print FILE "Deleted -> " . $file2 . "\n"; } } print FILE "\n\n"; print "Backups older than 1 day have been successfully deleted! Se +e LOG file for more details.\n"; $log_written = 1; }
All advice appreciated thanks EDIT: I should mention that unlink works if I type in: "unlink cmc_rules-Mon_Dec_12_12_31_01_EST_2012" Just not via the script

Replies are listed 'Best First'.
Re: Unlink works in windows, not in Linux?
by eyepopslikeamosquito (Chancellor) on Dec 12, 2012 at 04:53 UTC
Re: Unlink works in windows, not in Linux?
by Athanasius (Chancellor) on Dec 12, 2012 at 04:03 UTC

    I don’t know of any reason why unlink should fail under Linux, and in any case, from the fact that there is no error message (i.e., die is not called), it seems unlikely that unlink is the problem. Try running something like this:

    for ( <grm_backup-*_*_*_*_*_*_EST_*.bak>, <backup_db-*_*_*_*_*_*_EST_*.bak> ) { printf "File '%s' was last modified %d days ago\n", $_, -M; }

    to verify that there are files meeting the criterion for deletion.

    Note also that the until loop is doing nothing. It is equivalent to:

    while ($log_written == 0) { ... $log_written = 1; }

    which guarantees that there will only ever be a single pass.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Unlink works in windows, not in Linux?
by rovf (Priest) on Dec 12, 2012 at 10:00 UTC
    Honestly, I don't think that the file is not deleted, but you still don't the error message. I suggest you replace

    unlink $file or die "\nCould not delete the backup $file: $!";
    print FILE "Now going to delete $file\n"; if(unlink $file) { print FILE "$file has been removed (says 'unlink')\n"; if(-e $file) { print FILE "Surprise, surprise! $file is still here\n"; } } else { print FILE "'unlink' says: $!\n"; if(-e $file) { print FILE "And indeed, we still see $file\n"; } else { print FILE "A miracle occured! $file is gone!\n"; } }

    Ronald Fischer <>
Re: Unlink works in windows, not in Linux?
by tobyink (Abbot) on Dec 12, 2012 at 07:07 UTC

    Does the user running the Perl script have write permission to the files in question, and their directory?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Unlink works in windows, not in Linux?
by Anonymous Monk on Dec 12, 2012 at 17:05 UTC
    unlink $file or die Fudge("unlink q($file)"); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008432]
Approved by LanX
[Discipulus]: and in the wanted sub you just calculate the duretion and add it the total, stop
[shmem]: sub wanted { push @files, $File::Find::name if $File::Find::name =~/\.mp3$/ }
[Lady_Aleena]: shmem, that is understandable! The two examples in File::Find don't make sense to me on a quick glance.
[marioroy]: LA if the find worked from Unix command line, or does it not. Likely a quoting issue inside Perl qx.
[Lady_Aleena]: marioroy, the find worked fine at the command line.
[marioroy]: LA, yeah. than there's no reason why it cannot work inside qx. But chatting is hard in PM. I cannot see the code now.
[shmem]: Lady_Aleena: sometimes a quick glance isn't enough.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-04-23 20:54 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.