Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively find all php files not starting with a comment

Is there any useful combination of commands (sed/grep/find etc.) I can use for detecting .php files not starting with a comment? I could write a little script of course, but I'd rather use shell commands.

Matching pattern:

<?php
/*

I'd like searching in the contents of the file, not the file names.

I have to deal with a hacked website where code-injection follows a certain pattern.

<?php $code....
/*

or

<?php
$code....
/*
like image 275
christian Avatar asked Dec 10 '25 05:12

christian


2 Answers

Using gnu grep you can use this recursive search:

grep -rvlz $'^[[:space:]]*<?php\n/\*' --include='*.php'
like image 98
anubhava Avatar answered Dec 13 '25 01:12

anubhava


This will detect all php files that start with a php tag;

find ./ -iname '*.php' | xargs head -v -n 1 | grep -B 1 '<?php'
  • Find all files with php extensions.
  • head the first line and include the filename.
  • grep this output to find any files that start with php tag.
  • -B 1: keep 1 line before the match so we get the filename.

This is quick and dirty, you can get fancy to make the output nicer or make it more robust.

like image 24
Dan Avatar answered Dec 13 '25 02:12

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!