Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files which has the same prefix

Tags:

file

php

unlink

$prefix = 'something_prefix'; unlink($prefix.'.*'); 

the code above is not working, but I see some code like this below works just fine

unlink('*.jpg'); 

why? I am wonder is this going to work?

unlink('*.*'); 

how to delete the files which they begin with the same string? like this

same123.jpg sametoo.png samexxx.gif 

they all begins with the string "same" but ends with different extension, how to do this?

I alread have a cheap way to do this, but I wonder if there is any better solution?

like image 316
castiel Avatar asked Mar 18 '13 03:03

castiel


People also ask

How do I delete multiple files with same name?

Alternatively, head to the folder containing the files you want to delete, hit Shift + Right Click, and select Open a command window here. Then input "del [filename]" and press Enter.

How do I remove a file with the name '- something?

How do I remove or access a file with the name '-something' or containing another strange character ? If your file starts with a minus, use the -- flag to rm; if your file is named -g, then your rm command would look like rm -- -g.

How do I delete multiple files with the same name in Linux?

To delete multiple files at once, use the rm command followed by the file names separated by space. When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.


1 Answers

Try this code:

$mask = 'your_prefix_*.*'; array_map('unlink', glob($mask)); 

p.s. glob() requires PHP 4.3.0+

like image 121
Raptor Avatar answered Oct 26 '22 09:10

Raptor