Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to be able to append the text <?php to the beginning of php files in a list.

share|improve this question
Are you asking for PHP code? What have you tried so far? – Karan Jun 18 '13 at 22:50
Sorry had to change the text it failed to print everything after the <?php line until I added some backticks to it. Most of the research I studied online is done with sed or awk and a one example was in PERL. I am not focused so much on which language to use so long as I can get this problem solved. Would have been nice if Clam AV could have fixed this infection. – supernix Jun 18 '13 at 23:06
It'd also be nice if your question stated what OS . . . it sounds like the actual task you want to do is add a string of text at the beginning of several files, and you have a list of the filenames you want to modify. Is that correct? – ernie Jun 18 '13 at 23:28
That is correct. The topic has the OS, it is a CentOS Linux install. Yes the text file has the folder location and the filename in a line by line format. I ran a cleanup script to clean eval base64 code from the files then it made a text file with the files that had been cleaned only problem was that it didn't put the <?php back at the top of the files it fixed. – supernix Jun 19 '13 at 0:56

You say 'append the text <?php', but I think you mean 'prepend'.

Maybe I misunderstood the question, but this should solve your problem:

for FileName in * ; do (echo "<?php" ; cat $FileName) > corrected_$FileName ;done

That will create a bunch of files with same names except prefixed with 'corrected_'. If you want to alter files themselves then you could do this:

for FileName in * ; do (echo "<?php" ; cat $FileName) > /tmp/corrected ; mv -f /tmp/corrected $FileName ;done

...but be sure that's what you want.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.