【发布时间】:2015-06-24 16:11:36
【问题描述】:
我正在创建某种外壳来删除备份文件。我使用关键字“bkp”创建原始文件的备份。它可以是文件名中的任何位置,例如 ["abc.bkp.ctp", "abc-bkp.ctp", abc_bkp.ctp", "bkp_abc.ctp"]。我的意思是任何方式.我希望使用shell删除所有文件。我正在使用Cakephp的文件N文件夹类“http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html”。我将如何编写正则表达式来搜索这些文件。
我的 shell 逻辑是什么。
public function removeBkp() {
$path = BASE_PATH . DS . APP_DIR . DS;
$count = 0;
$this->out("", 2);
$this->out("-------------------------------------", 1);
$this->out("Path : " . $path, 1);
$this->out("FILES DETAILS", 1);
$this->out("-------------------------------------", 2);
$dir = new Folder($path);
// Need to seach bkp files here
$defaultFiles = $dir->findRecursive("(bkp)");
$results = $defaultFiles;
if ($results == null || empty($results)) {
$this->out("No file to delete.", 3);
} else {
foreach ($results as $file) {
// unlink($file);
$this->out("File removed - " . $file, 1);
$count++;
}
$this->out("Total files: " . count($results), 1);
}
}
【问题讨论】:
-
.*bkp.*[.]ctp之类的东西应该可以。 Demo here.