【问题标题】:Find file name recursively that contains matched pattern or by 99b and then delete递归查找包含匹配模式或按 99b 的文件名,然后删除
【发布时间】:2021-08-05 01:03:20
【问题描述】:

我的一台服务器上的一些网站被黑了。我已经清理了它们,但我注意到它们在每个目录中添加了一个 .htaccess 文件,我需要查看并删除它。

到目前为止我有这个。这列出了所有找到的匹配模式匹配的文件

find . -name ".htaccess" | xargs grep -i '<FilesMatch ".(py|exe|phtml|php|PhP|php5|suspected)$">'

返回找到的文件列表以供参考 .htaccess 文件的内容是:

<FilesMatch ".(py|exe|phtml|php|PhP|php5|suspected)$">
  Order Allow,Deny
  Deny from all
</FilesMatch>

在终端中执行 ls -al 时,文件大小始终为 99

我不确定最安全的方法,也许也可以添加 -size 但我一直在尝试但没有成功,所以最好仔细检查一下。

我主要挣扎的是执行删除找到的文件。

【问题讨论】:

    标签: linux bash shell ssh terminal


    【解决方案1】:

    您可能正在寻找类似的东西?

    #!/bin/bash
    # Loop over each file spit out by the find command
    while IFS= read -r -d '' ht_file; do
        # If the file contents match the pattern, delete it
        if grep -q -i '<FilesMatch ".(py|exe|phtml|php|PhP|php5|suspected)$">' "$ht_file"; then
            rm -f "$ht_file"
        fi
    done < <(find . -name ".htaccess" -print0)
    

    【讨论】:

    • 不确定它是否有效,应该在运行前检查文件但没有输出任何结果或者它不是故意的?
    • rm -f 不打印任何输出,因此您不会看到任何内容。您可以尝试在rm -f 之前添加echo "Deleting $ht_file" 以查看正在删除的内容。如果之前成功了,就不会有任何匹配了!
    • 您不应发布可能导致不可逆转的数据丢失的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2022-06-10
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多