I have a lot of files that got injected on a shared server. I need to replace the first line of all PHP files with an opening php tag if the first line exceeds 250 characters. Is there a simple-ish command that does this in linux?
Asked
Active
Viewed 36 times
0
-
1Possible duplicate of [Website hacked, how to remove malicious code with SED / GREP](http://stackoverflow.com/questions/28317990/website-hacked-how-to-remove-malicious-code-with-sed-grep) – tripleee Jul 22 '16 at 19:12
-
1See also http://meta.stackoverflow.com/questions/314002/how-to-cope-with-help-ive-been-hacked-questions – tripleee Jul 22 '16 at 19:16
1 Answers
0
Use sed
:
sed -i '1s/^.\{250,\}$/<?php/g' *.php
This will replace only the first line (1
before the s/
) if over 250 chars (.\{250, \}
) with <?php
in all php files in current directory.

Krzysztof Krasoń
- 26,515
- 16
- 89
- 115