you may easily use RecursiveDirectoyIterator
to do this as follows :
$directory = 'test/';
$iterator = new RecursiveDirectoryIterator($directory);
$directoryIterator = new RecursiveIteratorIterator($iterator);
foreach ($directoryIterator as $file) {
$octalPermission = substr(decoct($file->getPerms()), -4);
$extension = $file->getExtension();
if (!$file->isDir() && $octalPermission == '0777' && $extension == 'php') {
// do some stuff
unlink($file->getPathname());
}
}
this will recursively
iterate over your php file within a specific directory with a specific permission and delete it .
to use it for only current directory -not recursively- change this :
$iterator = new RecursiveDirectoryIterator($directory);
$directoryIterator = new RecursiveIteratorIterator($iterator);
by the following class :
$directoryIterator = new DirectoryIterator($directory);
Update
However , as long as the OP asks to do this using terminal not php,
you may use the following command :
find ./test/ -type f -name "*.php" -perm 0777 | xargs rm
Update 2 :
to checkout first for directories with permission 777 , as follows :
find ./test/ -type d -perm 0777 | xargs -I {} find {} -type f -name "*.php" -perm 0777 | xargs rm
Update 3 :
to make this command applies in files with spaces/special characters , you may use the -d
-delimiter option to set it to \n
as follows :
find ./test/ -type d -perm 0777 \
| xargs -I {} find {} -type f -name "*.php" -perm 0777 \
| xargs -d '\n' rm
// ^^^^^^^ notice this
this will find file name with php extension and permission 0777 then delete them.
Update 4 (31 Mar 2017)
for any of the previous commands , when you try to use xargs
command with rm
command , if there is no files exists in your search criteria , terminal may prompt an error like following :
rm: missing operand
to avoid this , you will need to add the -r
option to xargs :
-r, --no-run-if-empty if there are no arguments, then do not run COMMAND;
you may use it as follows :
xargs -r -d '\n' rm