2

I'm looking to scan a directory for files named *.php, and then execute each one with the PHP preprocessor, and capture the output to the same location as the file, with the same filename, only .html extension instead of .php.

so far I have:

find . -type f -name "*.php" | xargs -I {} php {} >> '{ dirname {}; basename {} .php; }'

thanks for your help on this bash puzzle !

2
  • Why not do it with PHP itself instead of Bash? Commented Sep 20, 2012 at 22:02
  • imho its what bash does best. Then again the beer tastes the same if you open it with a bottle opener or a hammer. Commented Sep 20, 2012 at 22:12

2 Answers 2

1

Think this works...

#!/bin/bash
FILES=/var/www/*.php
for f in $FILES
do
  echo "Running script $f..."
  php $f > $f.out
done

Maybe needing full path to bin where your php lives.

EDIT:

php $f > ${f%.php}.html
Sign up to request clarification or add additional context in comments.

3 Comments

it'll be doing php whatever.php > whatever.php.out. need to strip off the extension for the .out part.
True. But doesn't bother me or cat. But now I do see the OP wants name.html
yes, I'm aiming for the extension .html INSTEAD of .php, preserving directory structure to output folder.
0

You'd be better off with using -exec directly in find. xargs is for when you need to pass bulk parameters, while you're looking for something that's more per-file. e.g.

find ... -exec "php {} > $(basename {}).html;"

or something similar. Been a while since I've had to use bash, so that almost certainly won't work, but should get you started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.