0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Diemauerdk
  • 5,238
  • 9
  • 40
  • 56

2 Answers2

5

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

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

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • It works well thx :). But why wouldn't you recommend this way? – Diemauerdk Jun 21 '12 at 13:30
  • I don't exactly agree in what is "pro" and "con" in this list, but you can see good list of **differences** in [this question](http://stackoverflow.com/q/1506801/936986). – Oleg V. Volkov Jun 21 '12 at 13:34
0

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.

starbolin
  • 840
  • 5
  • 13