[英]How to delete all files except large list from database?
我在文件系統中存儲了大量文件,其中一些是mp4,jpg和pdf
我想刪除所有文件,除了其中一些是從mysql查詢中提取的文件。 例如:RandomMovie_04.mp4,AwesomeImage.jpg
這是我的文件結構
files/
messages/
images/
45/ --> this is the ID in the DB
46/
articles/
images/
124/
125/
videos/
124/
207/
我不想刪除的文件是從數據庫中提取的
我正在使用PHP(CakePHP)和MySQL
嘗試了一下,它只適用於一個文件,但是我找不到用文件列表執行此操作的方法
find * -maxdepth 5 -not -name "RandomMovie_04.mp4" -type f -exec ls "{}" \; > list.txt
這工作了兩個
find * -maxdepth 5 -not -name "RandomMovie_04.mp4" -not -name "AwesomeImage.jpg" -type f -exec ls "{}" \; > list.txt
我必須對所有排除的文件執行此操作(在php腳本中)? 例如-not -name文件A -not -name文件B -not -name to_the_infinite ...
謝謝
對不起,如果我的英語很爛:(
如果可以從PHP腳本中獲取名稱數組,則可以為find
創建字符串,如下所示:
<?php
$files = array("RandomMovie_04.mp4", "AwesomeImage.jpg", "GreatDoc.pdf");
echo implode(' -or ', array_map(function($x) { return "-name '$x'"; }, $files));
哪個輸出:
-name 'RandomMovie_04.mp4' -or -name 'AwesomeImage.jpg' -or -name 'GreatDoc.pdf'
您的find
命令可能是:
find /path/to/dir -not \( $(php files.php) \) -delete
find
將刪除所有未找到其名稱的文件。
請注意,您應該從單獨的目錄運行此文件,否則將刪除PHP文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.