【问题标题】:How to delete all files except large list from database?如何从数据库中删除除大列表之外的所有文件?
【发布时间】:2014-08-11 15:34:37
【问题描述】:

我在文件系统中存储了大量文件,其中一些是 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 fileA -not -name fileB -not -name to_the_infinite...

谢谢

对不起,如果我的英语很烂:(

【问题讨论】:

    标签: php mysql linux bash find


    【解决方案1】:

    如果您可以从 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 文件将被删除。

    【讨论】:

    • 非常感谢 Tom :),最后我做到了:$fullCmd = "find ".WWW_ROOT."files/ -maxdepth 4 -not ".$cmd." -type f -delete"; shell_exec($fullCmd); 其中 $cmd 是一个串联字符串(内爆)
    • @mallendeo 考虑接受此答案或发布您自己的答案并接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2014-08-24
    • 2010-09-07
    • 2018-01-15
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多