Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files using wildcard - exec vs unlink

Tags:

php

unlink

exec

I'm working on a php script, where I want to delete some files from a given folder using wildcard (*).
I've found some working examples like this one, where unlink() and glob() function are used.

Now, i was wondering, would it also be ok to delete the files using the exec function and a command like rm -f /path/to/folder/_prefix_* ?
Are there any security risks taken using this?
And if it is ok, would it be better in terms of performance?

EDIT:
So, from the first answers i can see that indeed, using exec could be an acceptable solution.
What about performance issues? Is there any chance the exec option could be better (faster/less demanding) over the glob/unlink technique?

Thank you in advance

like image 389
CdB Avatar asked Sep 03 '12 21:09

CdB


People also ask

How do I bulk delete files in Linux?

To delete multiple files at once, simply list all of the file names after the “rm” command. File names should be separated by a space. With the command “rm” followed by multiple file names, you can delete multiple files at once.

How do I delete multiple files in putty?

To delete multiple files at once, use the rm command followed by the file names separated by space.

How do I remove multiple files from a directory in Linux?

The rm command in Linux removes files and directories. Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces. The different rm command options include: - f : Forces the removal of all files or directories.


1 Answers

Because there is no chance for user-supplied data to be injected, there is no security issue in using exec over glob/unlink. However, using glob/unlink allows you to define exceptions:

foreach(glob("delete/*") as $f) {
    if( $f == "delete/notme.txt") continue;
    unlink($f);
}

And exec is often disabled on shared servers so glob/unlink is more portable. If you have a dedicated setup and don't intend on giving it up, you don't need to worry about that.

like image 148
Niet the Dark Absol Avatar answered Sep 28 '22 08:09

Niet the Dark Absol