Diemauerdk
Diemauerdk

Reputation: 5948

How to delete files based on the extension in perl?

I have a problem regarding deletion of files in Perl. I want to delete all files within a folder with the extension .log. Is there a smart way to do this in Perl?

I haven't got much experience coding perl.

Upvotes: 0

Views: 1292

Answers (2)

starbolin
starbolin

Reputation: 840

I like Oleg's, it's mad short. I usually hack up something like:

$  perl -e 'foreach my $f (@ARGV){ print `ls -l $f`; unlink $f }' *.log
-rw-r--r-- 1 The Genius None 0 Jun 21 06:15 bar.log
-rw-r--r-- 1 The Genius None 0 Jun 21 06:14 foo.log

Cause I can't remember all the commands and it's ease to add in tests and regexes. Also this prints out some indication of what it did. Choose your coffee.

Upvotes: 0

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

Fast and dirty: unlink glob('*.log');.

I'd recommend manual loop with opendir/readdir over directory for more control though.

Upvotes: 5

Related Questions