2

How do I delete files within a temp folder at only have the .jpg extension.

Here is what I tried;

unlink("../httpdocs/Temp/*.jpg);
Blnukem
  • 173
  • 3
  • 13

4 Answers4

13

You need to apply the glob function to the pattern, for example:

unlink glob "../httpdocs/Temp/*.jpg";

This is covered in the unlink and glob docs. Unlink itself expects a list of files to process. The glob function will, to quote the docs:

In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
5

martin clayton has given a good answer. To give proper warnings for failed file deletions might be a good idea, however, in which case a loop is better than using the list form of unlink:

unlink or warn "$_: $!" for glob "../httpdocs/Temp/*.jpg"
TLP
  • 66,756
  • 10
  • 92
  • 149
2

You can try this.. You need to use glob for removing files..

chdir "../httpdocs/Temp"
unlink glob "*.jpg"
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

I would suggest to operate in a while loop (scalar context) as using glob in list context can raise memory consumption, depending on number of dentries.

while( my $dentry = <../httpdocs/Temp/*.jpg> ) {
    unlink $dentry or die( "Couldn't delete the $dentry file: $!" );
}