简体   繁体   中英

Delete whole Directory if a Specefic File found in it using Find or Grep Command

I'm looking for Help to Delete whole Directory if a Specefic File found in it using find or grep Command

I'm able to search a file contained on specefic text using this Command

grep -iRl "Hacked by Hun73rCL4W"

在此处输入图像描述

As i Found that index (1).html file consist this specefic text i have searched.

在此处输入图像描述

Now, Not only I want to remove the index (1).html file but also want to remove it's all siblings files created by the hacker and its parent folder which is /uixuqkbxmy/ and /trwzqrpaaq/ in my case.

for that purpose, I have created and search many commands like below but all goes in vain not worked.

1. Search file Using find and grep command Together

find -type f -exec grep -l "Hacked by Hun73rCL4W" {} \;

it only helped me to find the filename and it's path.

2. Search and Delete

find -type f -exec grep -l "Hacked by Hun73rCL4W" {}; xargs rm -rf

find -type f -exec grep -l "Hacked by Hun73rCL4W" {}; -delete

these commands only helped me to delete a specific file that is being searched.

3. These commands helped me to Print Parent Directory Name.

find -type f -exec grep -l "Hacked by Hun73rCL4W" {}; -printf '%h\n'

Now I'm looking for a Solution a Command something like this below

find -type f -exec grep -l "Hacked by Hun73rCL4W" {}; -printf '%h\n' -exec rm -rf "{}";

  1. It Should search a File that contained Specefic Text inside.
  2. It should delete this file including it's sibling files and it's parent directory

How about this?

grep -ril "Hacked by Hun73rCL4W" | awk -F/ '{$NF="";print}' OFS='/' | xargs rm -rf

Seems to work for me:

$ mkdir -p public_html/wp-content/plugins/{awerwear,asdfse3r,sdfgeh}
$ vim public_html/wp-content/plugins/{awerwear,asdfse3r,sdfgeh}/index\ \(1\).html
$ grep -ril "Hacked by Hun73rCL4W"
public_html/wp-content/plugins/sdfgeh/index (1).html
public_html/wp-content/plugins/asdfse3r/index (1).html
public_html/wp-content/plugins/awerwear/index (1).html
$ grep -ril "Hacked by Hun73rCL4W" | awk -F/ '{$NF="";print}' OFS='/'
public_html/wp-content/plugins/sdfgeh/
public_html/wp-content/plugins/asdfse3r/
public_html/wp-content/plugins/awerwear/
$ grep -ril "Hacked by Hun73rCL4W" | awk -F/ '{$NF="";print}' OFS='/' | xargs rm -rf 
$ find
.
./pines
./public_html
./public_html/wp-content
./public_html/wp-content/plugins

PS: This does not invalidate my comment on your question; you will not be safe or "unhacked" by just removing those

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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