简体   繁体   English

在子文件夹中搜索文件中的字符串,然后删除删除匹配文件的父文件夹

[英]Search subfolders for string in files, and delete delete parent folder of matching file

I´ve been searching for quite some time now, but haven´t managed to find a solution to this. 我已经搜索了很长时间,但是还没有找到解决方案。 I have the following folder structure: 我具有以下文件夹结构:

/root/website1/file1.asp /root/website1/file1.asp
/root/website2/file2.txt /root/website2/file2.txt

I would like to search through all folders under /root/ (can be many levels) for files that are no older than 180 days that contain a specific string inside the file. 我想搜索/ root /下的所有文件夹(可以有多个级别),以查找不超过180天且文件中包含特定字符串的文件。 If a file contains the string, it´s parent folder (and the file itself) should be deleted. 如果文件包含字符串,则应删除其父文件夹(和文件本身)。 If file2.txt contained the string, then /root/website2 should be deleted. 如果file2.txt包含字符串,则应该删除/ root / website2。

I have gotten this far - but am quite stuck with passing dirname {} to an "rm -rf" command on the folder. 我已经走到了这一步-但是对于将dirname {}传递到文件夹上的“ rm -rf”命令还是很困惑。

find . 找 。 -name '*asp' -mtime -180 | 名称'* asp'-mtime -180 | xargs grep -l 'string' | xargs grep -l'字符串'| xargs -I{} dirname {} | xargs -I {}目录名{} | rm -rf rm -rf

In addition, it would be ideal if the search would move to the next folder after finding the first file containing the string - and not continue to search within the same folder. 此外,理想的情况是,在找到包含字符串的第一个文件之后,将搜索移至下一个文件夹,而不是继续在同一文件夹内搜索。 (ie just return and then delete unique folders, as there could be many matching files in a folder). (即,仅返回然后删除唯一的文件夹,因为一个文件夹中可能有许多匹配的文件)。 And lastly, it would be great if I could iterate through a list of strings in an input file to search for. 最后,如果我可以遍历输入文件中的字符串列表进行搜索,那将是很棒的。

Essentially, I´m cleaning up a very large amount of injected files on a web server. 本质上,我正在清理Web服务器上大量的注入文件。

Many thanks! 非常感谢!

You want to do something like this: 您想做这样的事情:

find . -name "*.txt" -exec grep -q deleteme {} \; -exec dirname {} \; | sort -u | xargs rm -rf

In the command above, find is given three expressions: -name "*.asp" , -exec grep -q string {} \\; 在上面的命令中,find提供了三个表达式: -name "*.asp" ,- -exec grep -q string {} \\; and -exec dirname {} \\; -exec dirname {} \\; . By default, find assumes that these three expressions are connected by -and operators. 默认情况下,find假定这三个表达式由-and运算符连接。 Find will evaluate the expressions, from left to right, until the outcome is known. Find将从左到右评估表达式,直到知道结果为止。 So, if the filename does not match *.asp, it does not run the second expression (because false and anything is false...). 因此,如果文件名不匹配* .asp,则它不会运行第二个表达式(因为为false,则为false ...)。 If grep -q string {} \\; 如果grep -q string {} \\; does not return 0, it will not run the third expression. 不返回0,则不会运行第三个表达式。 The third expression prints the dirname of anything that passed the first two expressions. 第三个表达式打印通过前两个表达式的任何内容的目录名。 You then pipe that into sort -u to remove duplicates, and xarg that to rm -rf . 然后,将其传送到sort -u以删除重复项,然后将xargrm -rf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM